-
Notifications
You must be signed in to change notification settings - Fork 364
/
NotificationReregistrationBackoffTest.java
216 lines (194 loc) · 6.54 KB
/
NotificationReregistrationBackoffTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*******************************************************************************
* Copyright (c) 2019 Rogier Cobben.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Rogier Cobben - initial creation
******************************************************************************/
package org.eclipse.californium.core.test;
import static org.eclipse.californium.elements.util.TestConditionTools.inRange;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapObserveRelation;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.config.CoapConfig;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.californium.elements.category.Medium;
import org.eclipse.californium.elements.config.Configuration;
import org.eclipse.californium.elements.rule.TestNameLoggerRule;
import org.eclipse.californium.elements.util.ExpectedExceptionWrapper;
import org.eclipse.californium.elements.util.TestScope;
import org.eclipse.californium.rule.CoapNetworkRule;
import org.eclipse.californium.rule.CoapThreadsRule;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
/**
* Test notification back-off.
*/
@RunWith(Parameterized.class)
@Category(Medium.class)
public class NotificationReregistrationBackoffTest {
@ClassRule
public static CoapNetworkRule network = new CoapNetworkRule(CoapNetworkRule.Mode.DIRECT,
CoapNetworkRule.Mode.NATIVE);
@ClassRule
public static CoapThreadsRule cleanup = new CoapThreadsRule();
@Rule
public TestNameLoggerRule name = new TestNameLoggerRule();
/**
* No exception expected by default
*/
@Rule
public ExpectedException exception = ExpectedExceptionWrapper.none();
/**
* Service resource name.
*/
private static final String TARGET = "lazy_resource";
/**
* Max age of resource state. [s]
*/
private static final long MAX_AGE = 1;
/**
* @return List of notification re-registration backoff times to test. [ms]
*/
@Parameters(name = "notification re-registration backoff = {0}")
public static Iterable<Long> backoffParams() {
if (TestScope.enableIntensiveTests()) {
return Arrays.asList(500L, 2000L, 4000L);
} else {
return Arrays.asList(500L);
}
}
/**
* Actual notification re-registration backoff to test. [ms]
*/
@Parameter
public long backoff;
/**
* Test client.
*/
private CoapClient client = null;
/**
* Test client endpoint;
*/
private CoapEndpoint clientEndpoint = null;
/**
* Start server
*/
@BeforeClass
public static void setupServer() {
CoapServer server = new CoapServer(network.getStandardTestConfig());
cleanup.add(server);
server.add(new LazyResource(TARGET));
server.start();
}
/**
* Create client.
*/
@Before
public void setupClient() {
Configuration config = Configuration.createStandardWithoutFile();
config.set(CoapConfig.NOTIFICATION_REREGISTRATION_BACKOFF, backoff, TimeUnit.MILLISECONDS);
clientEndpoint = new CoapEndpoint.Builder().setConfiguration(config).build();
client = new CoapClient();
client.setURI("coap://127.0.0.1/" + TARGET);
client.setEndpoint(clientEndpoint);
client.setTimeout(1000L);
}
/**
* Destroy client.
*/
@After
public void tearDownClient() {
if (clientEndpoint != null) {
clientEndpoint.destroy();
clientEndpoint = null;
}
if (client != null) {
client.shutdown();
client = null;
}
}
/**
* Test re-registration when notifications do not arrive.
*
* @throws InterruptedException should not occur
*/
@Test
public void reregistrationTest() throws InterruptedException {
final long expectedTimespanMillis = 3 * (MAX_AGE * 1000 + backoff);
CountingCoapHandler handler = new CountingCoapHandler();
long time = System.nanoTime();
CoapObserveRelation relation = client.observe(handler);
// wait for 3 re-registrations to happen, plus some grace time
boolean ready = handler.waitOnLoadCalls(4, expectedTimespanMillis + 200, TimeUnit.MILLISECONDS);
time = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - time);
relation.proactiveCancel();
assertTrue("cancel times out", handler.waitOnLoadCalls(5, 2000, TimeUnit.MILLISECONDS));
assertTrue("wrong number of responses/notifications", ready);
assertThat("timespan not in expected range", time, is(inRange(expectedTimespanMillis - 100L, expectedTimespanMillis + 200L)));
for (int index = 0; index < 5; ++index) {
CoapResponse response = handler.waitOnLoad(0);
assertNotNull("no response from server: ", response);
assertEquals("wrong responsecode: ", ResponseCode.CONTENT, response.getCode());
}
}
/**
* Service resource that does not bother to notify observing clients.
*
*/
public static class LazyResource extends CoapResource {
/**
* Constructor.
*
* @param name of the resource
*/
public LazyResource(String name) {
super(name);
this.setObservable(true);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.californium.core.CoapResource#handleGET(org.eclipse.
* californium.core.server.resources.CoapExchange)
*/
@Override
public void handleGET(CoapExchange exchange) {
Response response = new Response(ResponseCode.CONTENT);
// make an empty promise that observing clients get notified within
// MAX_AGE seconds
response.getOptions().setMaxAge(MAX_AGE);
exchange.respond(response);
}
}
}