@@ -20,10 +20,12 @@ class PollingEventSourceTest
20
20
extends
21
21
AbstractEventSourceTestBase <PollingEventSource <SampleExternalResource , HasMetadata >, EventHandler > {
22
22
23
+ public static final int DEFAULT_WAIT_PERIOD = 100 ;
24
+
23
25
private PollingEventSource .GenericResourceFetcher <SampleExternalResource > resourceFetcher =
24
26
mock (PollingEventSource .GenericResourceFetcher .class );
25
27
private final PollingEventSource <SampleExternalResource , HasMetadata > pollingEventSource =
26
- new PollingEventSource <>(resourceFetcher , 50L , SampleExternalResource .class ,
28
+ new PollingEventSource <>(resourceFetcher , 30L , SampleExternalResource .class ,
27
29
(SampleExternalResource er ) -> er .getName () + "#" + er .getValue ());
28
30
29
31
@ BeforeEach
@@ -35,7 +37,7 @@ public void setup() {
35
37
void pollsAndProcessesEvents () throws InterruptedException {
36
38
when (resourceFetcher .fetchResources ()).thenReturn (testResponseWithTwoValues ());
37
39
pollingEventSource .start ();
38
- Thread .sleep (100 );
40
+ Thread .sleep (DEFAULT_WAIT_PERIOD );
39
41
40
42
verify (eventHandler , times (2 )).handleEvent (any ());
41
43
}
@@ -45,7 +47,7 @@ void propagatesEventForRemovedResources() throws InterruptedException {
45
47
when (resourceFetcher .fetchResources ()).thenReturn (testResponseWithTwoValues ())
46
48
.thenReturn (testResponseWithOneValue ());
47
49
pollingEventSource .start ();
48
- Thread .sleep (150 );
50
+ Thread .sleep (DEFAULT_WAIT_PERIOD );
49
51
50
52
verify (eventHandler , times (3 )).handleEvent (any ());
51
53
}
@@ -54,11 +56,29 @@ void propagatesEventForRemovedResources() throws InterruptedException {
54
56
void doesNotPropagateEventIfResourceNotChanged () throws InterruptedException {
55
57
when (resourceFetcher .fetchResources ()).thenReturn (testResponseWithTwoValues ());
56
58
pollingEventSource .start ();
57
- Thread .sleep (250 );
59
+ Thread .sleep (DEFAULT_WAIT_PERIOD );
60
+
61
+ verify (eventHandler , times (2 )).handleEvent (any ());
62
+ }
63
+
64
+ @ Test
65
+ void propagatesEventOnNewResourceForPrimary () throws InterruptedException {
66
+ when (resourceFetcher .fetchResources ())
67
+ .thenReturn (testResponseWithOneValue ())
68
+ .thenReturn (testResponseWithTwoValueForSameId ());
69
+
70
+ pollingEventSource .start ();
71
+ Thread .sleep (DEFAULT_WAIT_PERIOD );
58
72
59
73
verify (eventHandler , times (2 )).handleEvent (any ());
60
74
}
61
75
76
+ private Map <ResourceID , Set <SampleExternalResource >> testResponseWithTwoValueForSameId () {
77
+ Map <ResourceID , Set <SampleExternalResource >> res = new HashMap <>();
78
+ res .put (primaryID1 (), Set .of (testResource1 (), testResource2 ()));
79
+ return res ;
80
+ }
81
+
62
82
private Map <ResourceID , Set <SampleExternalResource >> testResponseWithOneValue () {
63
83
Map <ResourceID , Set <SampleExternalResource >> res = new HashMap <>();
64
84
res .put (primaryID1 (), Set .of (testResource1 ()));
0 commit comments