1/*
2 * Copyright (C) 2012 Square, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.squareup.okhttp.internal.spdy;
17
18import org.junit.Test;
19
20import static com.squareup.okhttp.internal.spdy.Settings.DOWNLOAD_BANDWIDTH;
21import static com.squareup.okhttp.internal.spdy.Settings.DOWNLOAD_RETRANS_RATE;
22import static com.squareup.okhttp.internal.spdy.Settings.MAX_CONCURRENT_STREAMS;
23import static com.squareup.okhttp.internal.spdy.Settings.PERSISTED;
24import static com.squareup.okhttp.internal.spdy.Settings.PERSIST_VALUE;
25import static com.squareup.okhttp.internal.spdy.Settings.UPLOAD_BANDWIDTH;
26import static org.junit.Assert.assertEquals;
27import static org.junit.Assert.assertFalse;
28import static org.junit.Assert.assertTrue;
29
30public final class SettingsTest {
31  @Test public void unsetField() {
32    Settings settings = new Settings();
33    assertEquals(-3, settings.getUploadBandwidth(-3));
34  }
35
36  @Test public void setFields() {
37    Settings settings = new Settings();
38
39    assertEquals(-3, settings.getUploadBandwidth(-3));
40    settings.set(Settings.UPLOAD_BANDWIDTH, 0, 42);
41    assertEquals(42, settings.getUploadBandwidth(-3));
42
43    assertEquals(-3, settings.getDownloadBandwidth(-3));
44    settings.set(Settings.DOWNLOAD_BANDWIDTH, 0, 53);
45    assertEquals(53, settings.getDownloadBandwidth(-3));
46
47    assertEquals(-3, settings.getRoundTripTime(-3));
48    settings.set(Settings.ROUND_TRIP_TIME, 0, 64);
49    assertEquals(64, settings.getRoundTripTime(-3));
50
51    assertEquals(-3, settings.getMaxConcurrentStreams(-3));
52    settings.set(Settings.MAX_CONCURRENT_STREAMS, 0, 75);
53    assertEquals(75, settings.getMaxConcurrentStreams(-3));
54
55    assertEquals(-3, settings.getCurrentCwnd(-3));
56    settings.set(Settings.CURRENT_CWND, 0, 86);
57    assertEquals(86, settings.getCurrentCwnd(-3));
58
59    assertEquals(-3, settings.getDownloadRetransRate(-3));
60    settings.set(Settings.DOWNLOAD_RETRANS_RATE, 0, 97);
61    assertEquals(97, settings.getDownloadRetransRate(-3));
62
63    assertEquals(-3, settings.getInitialWindowSize(-3));
64    settings.set(Settings.INITIAL_WINDOW_SIZE, 0, 108);
65    assertEquals(108, settings.getInitialWindowSize(-3));
66
67    assertEquals(-3, settings.getClientCertificateVectorSize(-3));
68    settings.set(Settings.CLIENT_CERTIFICATE_VECTOR_SIZE, 0, 117);
69    assertEquals(117, settings.getClientCertificateVectorSize(-3));
70  }
71
72  @Test public void isPersisted() {
73    Settings settings = new Settings();
74
75    // Initially false.
76    assertFalse(settings.isPersisted(Settings.ROUND_TRIP_TIME));
77
78    // Set no flags.
79    settings.set(Settings.ROUND_TRIP_TIME, 0, 0);
80    assertFalse(settings.isPersisted(Settings.ROUND_TRIP_TIME));
81
82    // Set the wrong flag.
83    settings.set(Settings.ROUND_TRIP_TIME, PERSIST_VALUE, 0);
84    assertFalse(settings.isPersisted(Settings.ROUND_TRIP_TIME));
85
86    // Set the right flag.
87    settings.set(Settings.ROUND_TRIP_TIME, PERSISTED, 0);
88    assertTrue(settings.isPersisted(Settings.ROUND_TRIP_TIME));
89
90    // Set multiple flags.
91    settings.set(Settings.ROUND_TRIP_TIME, PERSIST_VALUE | PERSISTED, 0);
92    assertTrue(settings.isPersisted(Settings.ROUND_TRIP_TIME));
93
94    // Clear the flag.
95    settings.set(Settings.ROUND_TRIP_TIME, PERSIST_VALUE, 0);
96    assertFalse(settings.isPersisted(Settings.ROUND_TRIP_TIME));
97
98    // Clear all flags.
99    settings.set(Settings.ROUND_TRIP_TIME, 0, 0);
100    assertFalse(settings.isPersisted(Settings.ROUND_TRIP_TIME));
101  }
102
103  @Test public void persistValue() {
104    Settings settings = new Settings();
105
106    // Initially false.
107    assertFalse(settings.persistValue(Settings.ROUND_TRIP_TIME));
108
109    // Set no flags.
110    settings.set(Settings.ROUND_TRIP_TIME, 0, 0);
111    assertFalse(settings.persistValue(Settings.ROUND_TRIP_TIME));
112
113    // Set the wrong flag.
114    settings.set(Settings.ROUND_TRIP_TIME, PERSISTED, 0);
115    assertFalse(settings.persistValue(Settings.ROUND_TRIP_TIME));
116
117    // Set the right flag.
118    settings.set(Settings.ROUND_TRIP_TIME, PERSIST_VALUE, 0);
119    assertTrue(settings.persistValue(Settings.ROUND_TRIP_TIME));
120
121    // Set multiple flags.
122    settings.set(Settings.ROUND_TRIP_TIME, PERSIST_VALUE | PERSISTED, 0);
123    assertTrue(settings.persistValue(Settings.ROUND_TRIP_TIME));
124
125    // Clear the flag.
126    settings.set(Settings.ROUND_TRIP_TIME, PERSISTED, 0);
127    assertFalse(settings.persistValue(Settings.ROUND_TRIP_TIME));
128
129    // Clear all flags.
130    settings.set(Settings.ROUND_TRIP_TIME, 0, 0);
131    assertFalse(settings.persistValue(Settings.ROUND_TRIP_TIME));
132  }
133
134  @Test public void merge() {
135    Settings a = new Settings();
136    a.set(UPLOAD_BANDWIDTH, PERSIST_VALUE, 100);
137    a.set(DOWNLOAD_BANDWIDTH, PERSIST_VALUE, 200);
138    a.set(DOWNLOAD_RETRANS_RATE, 0, 300);
139
140    Settings b = new Settings();
141    b.set(DOWNLOAD_BANDWIDTH, 0, 400);
142    b.set(DOWNLOAD_RETRANS_RATE, PERSIST_VALUE, 500);
143    b.set(MAX_CONCURRENT_STREAMS, PERSIST_VALUE, 600);
144
145    a.merge(b);
146    assertEquals(100, a.getUploadBandwidth(-1));
147    assertEquals(PERSIST_VALUE, a.flags(UPLOAD_BANDWIDTH));
148    assertEquals(400, a.getDownloadBandwidth(-1));
149    assertEquals(0, a.flags(DOWNLOAD_BANDWIDTH));
150    assertEquals(500, a.getDownloadRetransRate(-1));
151    assertEquals(PERSIST_VALUE, a.flags(DOWNLOAD_RETRANS_RATE));
152    assertEquals(600, a.getMaxConcurrentStreams(-1));
153    assertEquals(PERSIST_VALUE, a.flags(MAX_CONCURRENT_STREAMS));
154  }
155}
156