remove timing sensitive portion from DefaultTopologyProviderTest.
Change-Id: I7fa496c421470520df4cb79c036ac8ea25b31c96
Showing
1 changed file
with
19 additions
and
15 deletions
1 | package org.onlab.onos.net.topology.impl; | 1 | package org.onlab.onos.net.topology.impl; |
2 | 2 | ||
3 | import com.google.common.collect.ImmutableSet; | 3 | import com.google.common.collect.ImmutableSet; |
4 | + | ||
4 | import org.junit.After; | 5 | import org.junit.After; |
5 | import org.junit.Before; | 6 | import org.junit.Before; |
6 | import org.junit.Test; | 7 | import org.junit.Test; |
... | @@ -21,10 +22,12 @@ import org.onlab.onos.net.topology.TopologyProviderService; | ... | @@ -21,10 +22,12 @@ import org.onlab.onos.net.topology.TopologyProviderService; |
21 | 22 | ||
22 | import java.util.List; | 23 | import java.util.List; |
23 | import java.util.Set; | 24 | import java.util.Set; |
25 | +import java.util.concurrent.Phaser; | ||
26 | +import java.util.concurrent.TimeUnit; | ||
27 | +import java.util.concurrent.TimeoutException; | ||
24 | 28 | ||
25 | -import static org.junit.Assert.assertEquals; | 29 | +import static org.junit.Assert.*; |
26 | -import static org.junit.Assert.assertNotNull; | 30 | +import static org.hamcrest.Matchers.*; |
27 | -import static org.onlab.junit.TestTools.assertAfter; | ||
28 | import static org.onlab.onos.net.NetTestTools.device; | 31 | import static org.onlab.onos.net.NetTestTools.device; |
29 | import static org.onlab.onos.net.NetTestTools.link; | 32 | import static org.onlab.onos.net.NetTestTools.link; |
30 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED; | 33 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED; |
... | @@ -41,6 +44,9 @@ public class DefaultTopologyProviderTest { | ... | @@ -41,6 +44,9 @@ public class DefaultTopologyProviderTest { |
41 | private TestLinkService linkService = new TestLinkService(); | 44 | private TestLinkService linkService = new TestLinkService(); |
42 | private TestTopoProviderService providerService; | 45 | private TestTopoProviderService providerService; |
43 | 46 | ||
47 | + // phase corresponds to number of topologyChanged called | ||
48 | + private Phaser topologyChangedCounts = new Phaser(1); | ||
49 | + | ||
44 | @Before | 50 | @Before |
45 | public void setUp() { | 51 | public void setUp() { |
46 | provider.deviceService = deviceService; | 52 | provider.deviceService = deviceService; |
... | @@ -66,27 +72,24 @@ public class DefaultTopologyProviderTest { | ... | @@ -66,27 +72,24 @@ public class DefaultTopologyProviderTest { |
66 | } | 72 | } |
67 | 73 | ||
68 | @Test | 74 | @Test |
69 | - public void basics() { | 75 | + public void basics() throws InterruptedException, TimeoutException { |
70 | - assertAfter(100, new Runnable() { | 76 | + assertEquals(1, topologyChangedCounts.awaitAdvanceInterruptibly(0, 1, TimeUnit.SECONDS)); |
71 | - @Override | ||
72 | - public void run() { | ||
73 | validateSubmission(); | 77 | validateSubmission(); |
74 | } | 78 | } |
75 | - }); | ||
76 | - } | ||
77 | 79 | ||
78 | @Test | 80 | @Test |
79 | - public void eventDriven() { | 81 | + public void eventDriven() throws InterruptedException, TimeoutException { |
80 | - assertAfter(100, new Runnable() { | 82 | + assertEquals(1, topologyChangedCounts.awaitAdvanceInterruptibly(0, 1, TimeUnit.SECONDS)); |
81 | - @Override | ||
82 | - public void run() { | ||
83 | validateSubmission(); | 83 | validateSubmission(); |
84 | + | ||
84 | deviceService.post(new DeviceEvent(DEVICE_ADDED, device("z"), null)); | 85 | deviceService.post(new DeviceEvent(DEVICE_ADDED, device("z"), null)); |
85 | linkService.post(new LinkEvent(LINK_ADDED, link("z", 1, "a", 4))); | 86 | linkService.post(new LinkEvent(LINK_ADDED, link("z", 1, "a", 4))); |
87 | + assertThat(topologyChangedCounts.awaitAdvanceInterruptibly(1, 1, TimeUnit.SECONDS), | ||
88 | + is(greaterThanOrEqualTo(2))); | ||
89 | + // Note: posting event, to trigger topologyChanged call, | ||
90 | + // but dummy topology will not change. | ||
86 | validateSubmission(); | 91 | validateSubmission(); |
87 | } | 92 | } |
88 | - }); | ||
89 | - } | ||
90 | 93 | ||
91 | 94 | ||
92 | private class TestTopoRegistry implements TopologyProviderRegistry { | 95 | private class TestTopoRegistry implements TopologyProviderRegistry { |
... | @@ -119,6 +122,7 @@ public class DefaultTopologyProviderTest { | ... | @@ -119,6 +122,7 @@ public class DefaultTopologyProviderTest { |
119 | @Override | 122 | @Override |
120 | public void topologyChanged(GraphDescription graphDescription, List<Event> reasons) { | 123 | public void topologyChanged(GraphDescription graphDescription, List<Event> reasons) { |
121 | graphDesc = graphDescription; | 124 | graphDesc = graphDescription; |
125 | + topologyChangedCounts.arrive(); | ||
122 | } | 126 | } |
123 | } | 127 | } |
124 | 128 | ... | ... |
-
Please register or login to post a comment