-
Notifications
You must be signed in to change notification settings - Fork 364
/
ObserveTest.java
1257 lines (1002 loc) · 45.5 KB
/
ObserveTest.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2015 Institute for Pervasive Computing, ETH Zurich and others.
*
* 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:
* Matthias Kovatsch - creator and main architect
* Martin Lanter - architect and re-implementation
* Dominique Im Obersteg - parsers and initial implementation
* Daniel Pauli - parsers and initial implementation
* Kai Hudalla - logging
* Achim Kraus - fixing race condition and visibility
* Achim Kraus (Bosch Software Innovations GmbH) - use CoapNetworkRule for
* setup of test-network
* Achim Kraus (Bosch Software Innovations GmbH) - add test for reregister
* issue #56.
* Introduce CountingHandler
* use expected= annotation for
* expected exceptions
* Achim Kraus (Bosch Software Innovations GmbH) - use MessageInterceptorAdapter
* Rogier Cobben - non and con observe (re)register and cancel tests
******************************************************************************/
package org.eclipse.californium.core.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.californium.TestTools;
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.coap.CoAP.Type;
import org.eclipse.californium.core.config.CoapConfig;
import org.eclipse.californium.core.coap.EmptyMessage;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.coap.Token;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.californium.core.network.EndpointManager;
import org.eclipse.californium.core.network.interceptors.MessageInterceptor;
import org.eclipse.californium.core.network.interceptors.MessageInterceptorAdapter;
import org.eclipse.californium.core.network.interceptors.MessageTracer;
import org.eclipse.californium.core.observe.Observation;
import org.eclipse.californium.core.observe.ObservationStore;
import org.eclipse.californium.core.observe.ObservationStoreException;
import org.eclipse.californium.core.observe.ObservationUtil;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.californium.elements.EndpointContext;
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.ExecutorsUtil;
import org.eclipse.californium.elements.util.NamedThreadFactory;
import org.eclipse.californium.rule.CoapNetworkRule;
import org.eclipse.californium.rule.CoapThreadsRule;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This test is valid for both drafts observe-08 and observe-09.
*/
/**
* This test tests that a server removes all observe relations to a client if a
* notification fails to transmit and that a new notification keeps the
* retransmission count of the previous notification.
* <p>
* The server has two observable resources X and Y. The client (5683) sends a
* request A to resource X and a request B to resource Y to observe both. Next,
* resource X changes and tries to notify request A. However, the notification
* goes lost (Implementation: ClientMessageInterceptor on the client cancels
* it). The server retransmits the notification but it goes lost again. The
* server now counts 2 failed transmissions. Next, the resource changes and
* issues a new notification. The server cancels the old notification but keeps
* the retransmission count (2) and the current timeout. After the forth
* retransmission the server gives up and assumes the client 5683 is offline.
* The server removes all relations with 5683.
* <p>
* In this test, retransmission is done constantly after 2 seconds (timeout does
* not increase). It should be checked manually that the retransmission counter
* is not reseted when a resource issues a new notification. The log should look
* something like this:
*
* <pre>
* 19 INFO [ReliabilityLayer$RetransmissionTask]: Timeout: retransmit message, failed: 1, ...
* 11 INFO [ReliabilityLayer$RetransmissionTask]: Timeout: retransmit message, failed: 2, ...
* Resource resX changed to "resX says hi for the 3 time"
* 19 INFO [ReliabilityLayer$RetransmissionTask]: Timeout: retransmit message, failed: 3, ...
* 11 INFO [ReliabilityLayer$RetransmissionTask]: Timeout: retransmit message, failed: 4, ...
* 17 INFO [ReliabilityLayer$RetransmissionTask]: Timeout: retransmission limit reached, exchange failed, ...
* </pre>
*/
@Category(Medium.class)
public class ObserveTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ObserveTest.class);
@ClassRule
public static CoapNetworkRule network = new CoapNetworkRule(CoapNetworkRule.Mode.DIRECT,
CoapNetworkRule.Mode.NATIVE);
static final String TARGET_X = "resX";
static final String TARGET_Y = "resY";
static final String RESPONSE = "hi";
static final AtomicInteger counter = new AtomicInteger();
@Rule
public CoapThreadsRule cleanup = new CoapThreadsRule();
@Rule
public TestNameLoggerRule name = new TestNameLoggerRule();
private CoapEndpoint serverEndpoint;
private MyResource resourceX;
private MyResource resourceY;
private MyObservationStore observations;
private final CountDownLatch waitforit = new CountDownLatch(1);
private String uriX;
private String uriY;
@Before
public void startupServer() {
cleanup.add(createServer());
}
@After
public void shutdownServer() {
Endpoint endpoint = EndpointManager.getEndpointManager().getDefaultEndpoint();
for (MessageInterceptor interceptor : endpoint.getInterceptors()) {
endpoint.removeInterceptor(interceptor);
}
}
@Test
public void testObserveLifecycle() throws Exception {
ClientMessageInterceptor interceptor = new ClientMessageInterceptor();
EndpointManager.getEndpointManager().getDefaultEndpoint().addInterceptor(interceptor);
// setup observe relation to resource X and Y
Request requestA = Request.newGet();
requestA.setURI(uriX);
requestA.setObserve();
requestA.send();
Request requestB = Request.newGet();
requestB.setURI(uriY);
requestB.setObserve();
requestB.send();
Response resp1 = requestA.waitForResponse(1000);
// ensure relations are established
assertNotNull("Client received no response", resp1);
assertTrue(resp1.getOptions().hasObserve());
assertEquals(1, resourceX.getObserverCount());
assertEquals(resp1.getPayloadString(), resourceX.currentResponse);
Response resp2 = requestB.waitForResponse(1000);
assertNotNull("Client received no response", resp2);
assertTrue(resp2.getOptions().hasObserve());
assertEquals(1, resourceY.getObserverCount());
assertEquals(resp2.getPayloadString(), resourceY.currentResponse);
LOGGER.info("\nObserve relation established, resource changes");
// change resource but lose response
Thread.sleep(50);
// change to "resX says Lifecycle for the 2 time"
resourceX.changed("Lifecycle");
// => trigger notification
// (which will go lost, see ClientMessageInterceptor)
// wait for the server to timeout, see ClientMessageInterceptor.
assertTrue(waitforit.await(1000, TimeUnit.MILLISECONDS));
Thread.sleep(500);
// the server should now have canceled all observer relations with 5683
// - request A to resource X
// - request B to resource Y
// check that relations to resource X AND Y have been canceled
assertEquals(0, resourceX.getObserverCount());
assertEquals(0, resourceY.getObserverCount());
observations.setStoreException(new ObservationStoreException("test"));
Request requestC = Request.newGet();
requestC.setURI(uriY);
requestC.setObserve();
requestC.send();
Response responseC = requestC.waitForResponse(1000);
assertNull("Client received unexpected response", responseC);
assertNotNull("send error expected", requestC.getSendError());
}
@Test
public void testObserveClient() throws Exception {
final AtomicInteger resetCounter = new AtomicInteger(0);
serverEndpoint.addInterceptor(new ServerMessageInterceptor(resetCounter));
resourceX.setObserveType(Type.NON);
int repeat = 3;
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
// onLoad is called asynchronous to returning the response
// therefore wait for one onLoad
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Relation canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
rel.reactiveCancel();
LOGGER.info("{} reactive canceled", uriX);
for (int i = 0; i < repeat; ++i) {
resourceX.changed("client");
Thread.sleep(50);
}
// still only one notification (the response) is received
assertFalse(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertEquals(repeat, resetCounter.get()); // repeat RST received
// no RST delivered (interceptor)
assertEquals(1, resourceX.getObserverCount());
}
@Test
public void testObserveClientDeleteResource() throws Exception {
serverEndpoint.addInterceptor(new ServerMessageInterceptor());
resourceX.setObserveType(Type.NON);
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
// onLoad is called asynchronous to returning the response
// therefore wait for one onLoad
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Relation canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
resourceX.delete();
LOGGER.info("{} deleted", uriX);
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
CoapResponse coapResponse = handler.responses.get(1);
assertEquals(ResponseCode.NOT_FOUND, coapResponse.getCode());
assertEquals(Type.CON, coapResponse.advanced().getType());
coapResponse = rel.getCurrent();
assertEquals(ResponseCode.NOT_FOUND, coapResponse.getCode());
assertEquals(Type.CON, coapResponse.advanced().getType());
}
@Test
public void testConObserveDefaultMessageType() throws Exception {
serverEndpoint.addInterceptor(new ServerMessageInterceptor());
resourceX.setObserveType(null);
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
// onLoad is called asynchronous to returning the response
// therefore wait for one onLoad
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Relation canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.ACK, rel.getCurrent().advanced().getType());
resourceX.changed("new");
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.CON, rel.getCurrent().advanced().getType());
}
@Test
public void testConObserveCancelMessageType() throws Exception {
serverEndpoint.addInterceptor(new ServerMessageInterceptor());
resourceX.setObserveType(null);
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
// onLoad is called asynchronous to returning the response
// therefore wait for one onLoad
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Relation canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.ACK, rel.getCurrent().advanced().getType());
resourceX.changed("new");
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.CON, rel.getCurrent().advanced().getType());
rel.proactiveCancel();
assertTrue(handler.waitOnLoadCalls(3, 1000, TimeUnit.MILLISECONDS));
assertTrue("Relation not canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.ACK, rel.getCurrent().advanced().getType());
assertFalse(rel.getCurrent().advanced().isNotification());
}
@Test
public void testConObserveNonCancelMessageType() throws Exception {
serverEndpoint.addInterceptor(new ServerMessageInterceptor());
resourceX.setObserveType(null);
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
// onLoad is called asynchronous to returning the response
// therefore wait for one onLoad
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Relation canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.ACK, rel.getCurrent().advanced().getType());
resourceX.changed("new");
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.CON, rel.getCurrent().advanced().getType());
rel.setConfirmable(false);
rel.proactiveCancel();
assertTrue(handler.waitOnLoadCalls(3, 1000, TimeUnit.MILLISECONDS));
assertTrue("Relation not canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
assertFalse(rel.getCurrent().advanced().isNotification());
}
@Test
public void testNonObserveCancelMessageType() throws Exception {
serverEndpoint.addInterceptor(new ServerMessageInterceptor());
resourceX.setObserveType(null);
CoapClient client = new CoapClient(uriX);
client.useNONs();
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
// onLoad is called asynchronous to returning the response
// therefore wait for one onLoad
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Relation canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
resourceX.changed("new");
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
rel.proactiveCancel();
assertTrue(handler.waitOnLoadCalls(3, 1000, TimeUnit.MILLISECONDS));
assertTrue("Relation not canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
assertFalse(rel.getCurrent().advanced().isNotification());
}
@Test
public void testNonObserveConCancelMessageType() throws Exception {
serverEndpoint.addInterceptor(new ServerMessageInterceptor());
resourceX.setObserveType(null);
CoapClient client = new CoapClient(uriX);
client.useNONs();
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
// onLoad is called asynchronous to returning the response
// therefore wait for one onLoad
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Relation canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
resourceX.changed("new");
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
rel.setConfirmable(true);
rel.proactiveCancel();
assertTrue(handler.waitOnLoadCalls(3, 1000, TimeUnit.MILLISECONDS));
assertTrue("Relation not canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.ACK, rel.getCurrent().advanced().getType());
assertFalse(rel.getCurrent().advanced().isNotification());
}
@Test
public void testNonObserveDefaultMessageType() throws Exception {
serverEndpoint.addInterceptor(new ServerMessageInterceptor());
resourceX.setObserveType(null);
CoapClient client = new CoapClient(uriX);
client.useNONs();
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
// onLoad is called asynchronous to returning the response
// therefore wait for one onLoad
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Relation canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
resourceX.changed("new");
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
}
@Test
public void testObserveNonErrorResponse() throws Exception {
serverEndpoint.addInterceptor(new ServerMessageInterceptor());
resourceX.setObserveType(Type.NON);
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
// onLoad is called asynchronous to returning the response
// therefore wait for one onLoad
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Relation canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.ACK, rel.getCurrent().advanced().getType());
resourceX.changed("new");
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
resourceX.responseCode.set(ResponseCode.PRECONDITION_FAILED);
resourceX.changed();
assertTrue(handler.waitOnLoadCalls(3, 1000, TimeUnit.MILLISECONDS));
assertNotNull("Response not received", rel.getCurrent());
assertEquals(ResponseCode.PRECONDITION_FAILED, rel.getCurrent().getCode());
assertEquals(Type.CON, rel.getCurrent().advanced().getType());
assertTrue("Observation not canceled by error", rel.isCanceled());
}
@Test
public void testObserveConErrorResponse() throws Exception {
serverEndpoint.addInterceptor(new ServerMessageInterceptor());
resourceX.setObserveType(Type.CON);
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
// onLoad is called asynchronous to returning the response
// therefore wait for one onLoad
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Relation canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.ACK, rel.getCurrent().advanced().getType());
resourceX.changed("new");
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.CON, rel.getCurrent().advanced().getType());
resourceX.responseCode.set(ResponseCode.PRECONDITION_FAILED);
resourceX.changed();
assertTrue(handler.waitOnLoadCalls(3, 1000, TimeUnit.MILLISECONDS));
assertNotNull("Response not received", rel.getCurrent());
assertEquals(ResponseCode.PRECONDITION_FAILED, rel.getCurrent().getCode());
assertEquals(Type.CON, rel.getCurrent().advanced().getType());
assertTrue("Observation not canceled by error", rel.isCanceled());
}
@Test(expected = IllegalArgumentException.class)
public void testObserveClearWithSuccessResponse() throws Exception {
serverEndpoint.addInterceptor(new ServerMessageInterceptor());
resourceX.setObserveType(Type.NON);
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
// onLoad is called asynchronous to returning the response
// therefore wait for one onLoad
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Relation canceled", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.ACK, rel.getCurrent().advanced().getType());
resourceX.changed("new");
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says new for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
resourceX.clearAndNotifyObserveRelations(ResponseCode.VALID);
}
@Test
public void testConObserveClientReregister() throws Exception {
resourceX.setObserveType(Type.NON);
MessageObserverCounterMessageInterceptor interceptor = new MessageObserverCounterMessageInterceptor();
EndpointManager.getEndpointManager().getDefaultEndpoint().addInterceptor(interceptor);
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Response not received", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
int counter = interceptor.getMessageObserverCounter();
resourceX.changed("client");
// assert notify received
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertFalse(rel.isCanceled());
assertEquals("\"resX says client for the 2 time\"", rel.getCurrent().getResponseText());
rel.reregister();
assertFalse(rel.isCanceled());
// assert reregister succeeded
assertTrue(handler.waitOnLoadCalls(3, 1000, TimeUnit.MILLISECONDS));
LOGGER.info("{} reregistered", uriX);
assertFalse(rel.isCanceled());
// resource not changed
assertEquals("\"resX says client for the 2 time\"", rel.getCurrent().getResponseText());
resourceX.changed("new client");
// assert notify received after reregister
assertTrue(handler.waitOnLoadCalls(4, 1000, TimeUnit.MILLISECONDS));
assertEquals("\"resX says new client for the 3 time\"", rel.getCurrent().getResponseText());
assertEquals(1, resourceX.getObserverCount());
assertEquals("message observer leak", counter, interceptor.getMessageObserverCounter());
}
@Test
public void testNonObserveClientReregister() throws Exception {
resourceX.setObserveType(Type.NON);
MessageObserverCounterMessageInterceptor interceptor = new MessageObserverCounterMessageInterceptor();
EndpointManager.getEndpointManager().getDefaultEndpoint().addInterceptor(interceptor);
CoapClient client = new CoapClient(uriX);
client.useNONs();
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Response not received", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
int counter = interceptor.getMessageObserverCounter();
resourceX.changed("client");
// assert notify received
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertFalse(rel.isCanceled());
assertEquals("\"resX says client for the 2 time\"", rel.getCurrent().getResponseText());
rel.reregister();
assertFalse(rel.isCanceled());
// assert reregister succeeded
assertTrue(handler.waitOnLoadCalls(3, 1000, TimeUnit.MILLISECONDS));
LOGGER.info("{} reregistered", uriX);
assertFalse(rel.isCanceled());
// resource not changed
assertEquals("\"resX says client for the 2 time\"", rel.getCurrent().getResponseText());
resourceX.changed("new client");
// assert notify received after reregister
assertTrue(handler.waitOnLoadCalls(4, 1000, TimeUnit.MILLISECONDS));
assertEquals("\"resX says new client for the 3 time\"", rel.getCurrent().getResponseText());
assertEquals(1, resourceX.getObserverCount());
assertEquals("message observer leak", counter, interceptor.getMessageObserverCounter());
}
@Test
public void testConObserveClientNonReregister() throws Exception {
resourceX.setObserveType(Type.NON);
MessageObserverCounterMessageInterceptor interceptor = new MessageObserverCounterMessageInterceptor();
EndpointManager.getEndpointManager().getDefaultEndpoint().addInterceptor(interceptor);
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Response not received", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.ACK, rel.getCurrent().advanced().getType());
int counter = interceptor.getMessageObserverCounter();
resourceX.changed("client");
// assert notify received
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertFalse(rel.isCanceled());
assertEquals("\"resX says client for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
rel.setConfirmable(false);
rel.reregister();
assertFalse(rel.isCanceled());
// assert reregister succeeded
assertTrue(handler.waitOnLoadCalls(3, 1000, TimeUnit.MILLISECONDS));
LOGGER.info("{} reregistered non-confirmable", uriX);
assertFalse(rel.isCanceled());
// resource not changed
assertEquals("\"resX says client for the 2 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
resourceX.changed("new client");
// assert notify received after reregister
assertTrue(handler.waitOnLoadCalls(4, 1000, TimeUnit.MILLISECONDS));
assertEquals("\"resX says new client for the 3 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
assertEquals(1, resourceX.getObserverCount());
rel.reregister();
assertFalse(rel.isCanceled());
// assert reregister succeeded
assertTrue(handler.waitOnLoadCalls(5, 1000, TimeUnit.MILLISECONDS));
LOGGER.info("{} reregistered confirmable", uriX);
assertFalse(rel.isCanceled());
// resource not changed
assertEquals("\"resX says new client for the 3 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
resourceX.changed("new client");
// assert notify received after reregister
assertTrue(handler.waitOnLoadCalls(6, 1000, TimeUnit.MILLISECONDS));
assertEquals("\"resX says new client for the 4 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
assertEquals(1, resourceX.getObserverCount());
// NON request results in one message-observer less (missing reliability
// layer observer).
assertEquals("message observer leak", counter - 1, interceptor.getMessageObserverCounter());
}
@Test
public void testNonObserveClientConReregister() throws Exception {
resourceX.setObserveType(Type.NON);
MessageObserverCounterMessageInterceptor interceptor = new MessageObserverCounterMessageInterceptor();
EndpointManager.getEndpointManager().getDefaultEndpoint().addInterceptor(interceptor);
CoapClient client = new CoapClient(uriX);
client.useNONs();
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
assertFalse("Response not received", rel.isCanceled());
assertNotNull("Response not received", rel.getCurrent());
assertEquals("\"resX says hi for the 1 time\"", rel.getCurrent().getResponseText());
int counter = interceptor.getMessageObserverCounter();
resourceX.changed("client");
// assert notify received
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertFalse(rel.isCanceled());
assertEquals("\"resX says client for the 2 time\"", rel.getCurrent().getResponseText());
rel.setConfirmable(true);
rel.reregister();
assertFalse(rel.isCanceled());
// assert reregister succeeded
assertTrue(handler.waitOnLoadCalls(3, 1000, TimeUnit.MILLISECONDS));
LOGGER.info("{} reregistered", uriX);
assertFalse(rel.isCanceled());
// resource not changed
assertEquals("\"resX says client for the 2 time\"", rel.getCurrent().getResponseText());
resourceX.changed("new client");
// assert notify received after reregister
assertTrue(handler.waitOnLoadCalls(4, 1000, TimeUnit.MILLISECONDS));
assertEquals("\"resX says new client for the 3 time\"", rel.getCurrent().getResponseText());
assertEquals(1, resourceX.getObserverCount());
rel.reregister();
assertFalse(rel.isCanceled());
// assert reregister succeeded
assertTrue(handler.waitOnLoadCalls(5, 1000, TimeUnit.MILLISECONDS));
LOGGER.info("{} reregistered confirmable", uriX);
assertFalse(rel.isCanceled());
// resource not changed
assertEquals("\"resX says new client for the 3 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.ACK, rel.getCurrent().advanced().getType());
resourceX.changed("new client");
// assert notify received after reregister
assertTrue(handler.waitOnLoadCalls(6, 1000, TimeUnit.MILLISECONDS));
assertEquals("\"resX says new client for the 4 time\"", rel.getCurrent().getResponseText());
assertEquals(Type.NON, rel.getCurrent().advanced().getType());
assertEquals(1, resourceX.getObserverCount());
// CON request results in extra message-observer from reliability layer.
assertEquals("message observer leak", counter + 1, interceptor.getMessageObserverCounter());
}
@Test(expected = IllegalStateException.class)
public void testObserveClientReregisterAfterReject() throws Exception {
resourceX.setObserveType(Type.NON);
resourceX.rejectNextGet();
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
assertTrue("Not rejected", rel.isCanceled());
rel.reregister();
}
@Test(expected = IllegalStateException.class)
public void testObserveClientReregisterAfterTimeout() throws Exception {
resourceX.setObserveType(Type.NON);
resourceX.delayNextGet(100);
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
client.setTimeout(1L);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
assertTrue("No timeout", rel.isCanceled());
client.setTimeout(null);
rel.reregister();
}
@Test
public void testObserveClientReregisterBeforeTimeout() throws Exception {
resourceX.setObserveType(Type.NON);
resourceX.delayNextGet(100);
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observe(handler);
assertFalse("Timeout", rel.isCanceled());
assertFalse("reregister not ignored", rel.reregister());
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
resourceX.changed("client");
// assert notify received
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
assertFalse("Response not received", rel.isCanceled());
}
@Test
public void testObserveClientReregisterAfterReregister() throws Exception {
resourceX.setObserveType(Type.NON);
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
CountingCoapHandler handler = new CountingCoapHandler();
CoapObserveRelation rel = client.observeAndWait(handler);
assertFalse("Response not received", rel.isCanceled());
assertTrue(handler.waitOnLoadCalls(1, 1000, TimeUnit.MILLISECONDS));
resourceX.delayNextGet(100);
// one more onLoad call
assertTrue("reregister not triggered", rel.reregister());
// one more onLoad call
assertFalse("reregister not ignored", rel.reregister());
assertTrue(handler.waitOnLoadCalls(2, 1000, TimeUnit.MILLISECONDS));
// one more onLoad call
resourceX.changed("client");
// assert notify received
assertTrue(handler.waitOnLoadCalls(3, 1000, TimeUnit.MILLISECONDS));
assertFalse("Response not received", rel.isCanceled());
}
/**
* Test case for CoapClient.observeAndWait(Request request, CoapHandler
* handler) exception handling.
*
* @throws Exception
*/
@Test(expected = IllegalArgumentException.class)
public void testObserveAndWaitExceptionHandling() throws Exception {
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
Request request = Request.newGet().setURI(uriX);
@SuppressWarnings("unused")
CoapObserveRelation rel = client.observeAndWait(request, new CountingCoapHandler());
}
/**
* Test case for CoapClient.observe(Request request, CoapHandler handler)
* exception handling.
*
* @throws Exception
*/
@Test(expected = IllegalArgumentException.class)
public void testObserveExceptionHandling() throws Exception {
CoapClient client = new CoapClient(uriX);
cleanup.add(client);
Request request = Request.newGet().setURI(uriX);
@SuppressWarnings("unused")
CoapObserveRelation rel = client.observe(request, new CountingCoapHandler());
}
private CoapServer createServer() {
// retransmit constantly all 200 milliseconds
Configuration config = network.createTestConfig().set(CoapConfig.ACK_TIMEOUT, 200, TimeUnit.MILLISECONDS)
.set(CoapConfig.ACK_INIT_RANDOM, 1f).set(CoapConfig.ACK_TIMEOUT_SCALE, 1f);
CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
builder.setInetSocketAddress(TestTools.LOCALHOST_EPHEMERAL);
builder.setConfiguration(config);
serverEndpoint = builder.build();
serverEndpoint.addInterceptor(new MessageTracer());
int count = counter.incrementAndGet();
CoapServer server = new CoapServer(config);
server.setExecutors(ExecutorsUtil.newScheduledThreadPool(//
config.get(CoapConfig.PROTOCOL_STAGE_THREAD_COUNT),
new NamedThreadFactory("CoapServer(main):" + count + "#")), //$NON-NLS-1$
ExecutorsUtil.newDefaultSecondaryScheduler("CoapServer(secondary):" + count + "#"), false);
server.addEndpoint(serverEndpoint);
resourceX = new MyResource(TARGET_X);
resourceY = new MyResource(TARGET_Y);
server.add(resourceX);
server.add(resourceY);
server.start();
uriX = TestTools.getUri(serverEndpoint, TARGET_X);
uriY = TestTools.getUri(serverEndpoint, TARGET_Y);
observations = new MyObservationStore();
// setup the client endpoint using the special observation store
builder = new CoapEndpoint.Builder();
builder.setInetSocketAddress(TestTools.LOCALHOST_EPHEMERAL);
builder.setConfiguration(config);
builder.setObservationStore(observations);
CoapEndpoint coapEndpoint = builder.build();
EndpointManager.getEndpointManager().setDefaultEndpoint(coapEndpoint);
return server;
}
private class ClientMessageInterceptor extends MessageInterceptorAdapter {