Skip to content

Commit c1de1e0

Browse files
weitehowenjunche
authored andcommitted
RUN-3975 added unit test case for IAB with reconnection. (openfin#19)
1 parent 85463ed commit c1de1e0

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package com.openfin.desktop;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.concurrent.BrokenBarrierException;
6+
import java.util.concurrent.CountDownLatch;
7+
import java.util.concurrent.CyclicBarrier;
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.concurrent.atomic.AtomicInteger;
10+
11+
import org.junit.AfterClass;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
17+
public class DesktopConnectionTest {
18+
private static Logger logger = LoggerFactory.getLogger(DesktopConnectionTest.class.getName());
19+
20+
private static final String DESKTOP_UUID = DesktopConnectionTest.class.getName();
21+
22+
@BeforeClass
23+
public static void setup() throws Exception {
24+
}
25+
26+
@AfterClass
27+
public static void teardown() throws Exception {
28+
}
29+
30+
@Test
31+
public void reconnect() throws Exception {
32+
// IAB subscription shouldn't survive reconnection
33+
34+
final AtomicInteger invokeCnt = new AtomicInteger(0);
35+
String topic1 = "myTopic1";
36+
String topic2 = "myTopic2";
37+
String message1 = "myMessage1";
38+
String message2 = "myMessage2";
39+
final CyclicBarrier connectLatch = new CyclicBarrier(2);
40+
final CountDownLatch disconnectLatch = new CountDownLatch(1);
41+
DesktopConnection conn = new DesktopConnection(DESKTOP_UUID);
42+
RuntimeConfiguration conf = new RuntimeConfiguration();
43+
conf.setRuntimeVersion(TestUtils.getRuntimeVersion());
44+
45+
DesktopStateListener listener = new DesktopStateListener() {
46+
47+
@Override
48+
public void onReady() {
49+
try {
50+
connectLatch.await();
51+
logger.debug("onReady");
52+
}
53+
catch (InterruptedException e) {
54+
e.printStackTrace();
55+
}
56+
catch (BrokenBarrierException e) {
57+
e.printStackTrace();
58+
}
59+
}
60+
61+
@Override
62+
public void onClose(String error) {
63+
disconnectLatch.countDown();
64+
logger.debug("onClose: " + error);
65+
}
66+
67+
@Override
68+
public void onError(String reason) {
69+
}
70+
71+
@Override
72+
public void onMessage(String message) {
73+
}
74+
75+
@Override
76+
public void onOutgoingMessage(String message) {
77+
}
78+
79+
};
80+
81+
conn.connect(conf, listener, 10);
82+
connectLatch.await(10, TimeUnit.SECONDS);
83+
84+
assertEquals(0, connectLatch.getNumberWaiting());
85+
86+
// create an app and let it sit through the reconnection.
87+
ApplicationOptions options = TestUtils.getAppOptions(null);
88+
Application application = TestUtils.runApplication(options, conn);
89+
90+
CountDownLatch listener1Latch = new CountDownLatch(1);
91+
// subscribe to a topic
92+
conn.getInterApplicationBus().subscribe("*", topic1, new BusListener() {
93+
94+
@Override
95+
public void onMessageReceived(String sourceUuid, String topic, Object payload) {
96+
logger.info("listener1 received: " + payload.toString());
97+
invokeCnt.incrementAndGet();
98+
if (message1.equals(payload.toString())) {
99+
listener1Latch.countDown();
100+
}
101+
}
102+
}, new AckListener() {
103+
@Override
104+
public void onSuccess(Ack ack) {
105+
if (ack.isSuccessful()) {
106+
try {
107+
conn.getInterApplicationBus().publish(topic1, message1);
108+
}
109+
catch (DesktopException e) {
110+
e.printStackTrace();
111+
}
112+
}
113+
}
114+
115+
@Override
116+
public void onError(Ack ack) {
117+
logger.error(ack.getReason());
118+
}
119+
});
120+
121+
listener1Latch.await(5, TimeUnit.SECONDS);
122+
assertEquals(0, listener1Latch.getCount());
123+
124+
try {
125+
conn.disconnect("junit test");
126+
}
127+
catch (DesktopException e) {
128+
e.printStackTrace();
129+
}
130+
131+
disconnectLatch.await(5, TimeUnit.SECONDS);
132+
133+
assertEquals(0, disconnectLatch.getCount());
134+
135+
// connect it again.
136+
connectLatch.reset();
137+
conn.connect(conf, listener, 10);
138+
connectLatch.await(10, TimeUnit.SECONDS);
139+
assertEquals(0, connectLatch.getNumberWaiting());
140+
141+
CountDownLatch listener2Latch = new CountDownLatch(1);
142+
143+
conn.getInterApplicationBus().subscribe("*", topic2, new BusListener() {
144+
145+
@Override
146+
public void onMessageReceived(String sourceUuid, String topic, Object payload) {
147+
listener2Latch.countDown();
148+
logger.info("listener2 received: " + payload.toString());
149+
}
150+
}, new AckListener() {
151+
@Override
152+
public void onSuccess(Ack ack) {
153+
if (ack.isSuccessful()) {
154+
try {
155+
conn.getInterApplicationBus().publish(topic1, message2);
156+
conn.getInterApplicationBus().publish(topic2, message2);
157+
}
158+
catch (DesktopException e) {
159+
e.printStackTrace();
160+
}
161+
catch (Exception e) {
162+
e.printStackTrace();
163+
}
164+
}
165+
}
166+
167+
@Override
168+
public void onError(Ack ack) {
169+
logger.error(ack.getReason());
170+
}
171+
});
172+
173+
//can kill the app now.
174+
Application app = Application.wrap(application.uuid, conn);
175+
app.close();
176+
177+
listener2Latch.await(5, TimeUnit.SECONDS);
178+
179+
assertEquals(0, listener2Latch.getCount());
180+
assertEquals(1, invokeCnt.get());
181+
}
182+
183+
}

0 commit comments

Comments
 (0)