Skip to content

Commit d988247

Browse files
committed
Fix flaky tests due to race condition in call state changes
1 parent e5b9f40 commit d988247

File tree

5 files changed

+47
-60
lines changed

5 files changed

+47
-60
lines changed

src/test/java/net/java/sip/communicator/service/protocol/mock/MockBasicTeleOpSet.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void answerCallPeer(CallPeer peer)
6565

6666
((MockCallPeer) peer).setState(CallPeerState.CONNECTED);
6767

68-
((MockCall) peer.getCall()).setCallState(CallState.CALL_IN_PROGRESS);
68+
((MockCall) peer.getCall()).setInProgress();
6969
}
7070

7171
@Override
@@ -108,11 +108,11 @@ public MockProtocolProvider getProtocolProvider()
108108
return protocolProvider;
109109
}
110110

111-
public synchronized MockCall createIncomingCall(String calee)
111+
public synchronized MockCall createIncomingCall(String callee)
112112
{
113113
MockCall incomingCall = new MockCall(this);
114114

115-
MockCallPeer peer = new MockCallPeer(calee, incomingCall);
115+
MockCallPeer peer = new MockCallPeer(callee, incomingCall);
116116

117117
incomingCall.addCallPeer(peer);
118118

src/test/java/net/java/sip/communicator/service/protocol/mock/MockCall.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,15 @@ public String toString()
7878
return super.toString() + " " + getProtocolProvider().getProtocolName();
7979
}
8080

81-
public void setCallState(CallState newState)
81+
public synchronized void setInProgress()
8282
{
83-
super.setCallState(newState);
83+
if (getCallState() != CallState.CALL_ENDED)
84+
{
85+
setCallState(CallState.CALL_IN_PROGRESS);
86+
}
87+
else
88+
{
89+
throw new RuntimeException("Call already ended");
90+
}
8491
}
8592
}

src/test/java/org/jitsi/jigasi/CallStateListener.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,21 @@ public void callStateChanged(CallChangeEvent evt)
4747
}
4848

4949
public void waitForState(Call watchedCall,
50-
CallState targetState,
51-
long timeout)
50+
CallState targetState)
5251
throws InterruptedException
5352
{
5453
this.targetState = targetState;
5554

56-
// FIXME: we can miss call state anyway ?(but timeout will release)
55+
watchedCall.addCallChangeListener(this);
5756
if (!targetState.equals(watchedCall.getCallState()))
5857
{
5958
synchronized (this)
6059
{
61-
watchedCall.addCallChangeListener(this);
62-
63-
this.wait(timeout);
60+
this.wait();
6461
}
6562
}
6663

6764
watchedCall.removeCallChangeListener(this);
68-
6965
assertEquals(targetState, watchedCall.getCallState());
7066
}
7167
}

src/test/java/org/jitsi/jigasi/CallsHandlingTest.java

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.jitsi.jigasi.xmpp.*;
2626
import org.jitsi.service.configuration.*;
2727
import org.jitsi.xmpp.extensions.rayo.*;
28-
import org.junit.jupiter.api.Test;
2928
import org.junit.jupiter.api.*;
3029
import org.jxmpp.jid.*;
3130
import org.jxmpp.jid.impl.*;
@@ -43,6 +42,7 @@
4342
* @author Pawel Domas
4443
* @author Nik Vaessen
4544
*/
45+
@Timeout(30)
4646
public class CallsHandlingTest
4747
{
4848
private static OSGiHandler osgi;
@@ -65,7 +65,6 @@ public static void setUpClass()
6565
osgi = new OSGiHandler();
6666
var fw = osgi.init();
6767
var start = System.nanoTime();
68-
Thread.sleep(5000);
6968
while (fw.getState() != Framework.ACTIVE)
7069
{
7170
if (System.nanoTime() - start > TimeUnit.SECONDS.toNanos(5))
@@ -139,7 +138,7 @@ public void testIncomingSipCall()
139138

140139
// SipGateway ought to accept incoming sip call
141140
// once JVB conference is joined.
142-
callStateWatch.waitForState(sipCall, CallState.CALL_IN_PROGRESS, 1000);
141+
callStateWatch.waitForState(sipCall, CallState.CALL_IN_PROGRESS);
143142

144143
SipGatewaySession session
145144
= osgi.getSipGateway().getActiveSessions().get(0);
@@ -155,7 +154,7 @@ public void testIncomingSipCall()
155154
CallManager.hangupCall(sipCall);
156155

157156
callStateWatch.waitForState(
158-
session.getJvbCall(), CallState.CALL_ENDED, 1000);
157+
session.getJvbCall(), CallState.CALL_ENDED);
159158
assertFalse(jvbConfRoom.isJoined());
160159
}
161160

@@ -202,7 +201,7 @@ public void testOutgoingSipCall()
202201
// Remote SIP peer accepts
203202
CallManager.acceptCall(sipCall);
204203

205-
callStateWatch.waitForState(sipCall, CallState.CALL_IN_PROGRESS, 1000);
204+
callStateWatch.waitForState(sipCall, CallState.CALL_IN_PROGRESS);
206205

207206
GatewaySessionAsserts sessionWatch = new GatewaySessionAsserts();
208207
sessionWatch.assertJvbRoomJoined(session, 2000);
@@ -213,16 +212,16 @@ public void testOutgoingSipCall()
213212

214213
assertNotNull(chatRoom);
215214
assertTrue(chatRoom.isJoined());
216-
callStateWatch.waitForState(jvbCall, CallState.CALL_IN_PROGRESS, 1000);
215+
callStateWatch.waitForState(jvbCall, CallState.CALL_IN_PROGRESS);
217216
assertEquals(CallState.CALL_IN_PROGRESS, jvbCall.getCallState());
218217

219218
// Now tear down, SIP calee ends the call
220219
// then XMPP cal should be terminated and MUC room left
221220

222221
CallManager.hangupCall(sipCall);
223222

224-
callStateWatch.waitForState(sipCall, CallState.CALL_ENDED, 1000);
225-
callStateWatch.waitForState(jvbCall, CallState.CALL_ENDED, 1000);
223+
callStateWatch.waitForState(sipCall, CallState.CALL_ENDED);
224+
callStateWatch.waitForState(jvbCall, CallState.CALL_ENDED);
226225
assertFalse(chatRoom.isJoined());
227226
}
228227

@@ -270,12 +269,11 @@ public void testFocusLeftTheRoomWithNoResume()
270269
// Focus will leave the room after inviting us to the conference
271270
focus.setLeaveRoomAfterInvite(true);
272271

273-
CallStateListener callStateWatch = new CallStateListener();
274-
275272
// Create incoming call
276-
MockCall sipCall = sipProvider.getTelephony().mockIncomingGatewayCall("calee", roomName);
273+
MockCall sipCall = sipProvider.getTelephony().mockIncomingGatewayCall("callee", roomName);
277274

278-
callStateWatch.waitForState(sipCall, CallState.CALL_ENDED, 2000);
275+
var callStateWatch = new CallStateListener();
276+
callStateWatch.waitForState(sipCall, CallState.CALL_ENDED);
279277

280278
// Now we expect SIP call to be terminated
281279
assertEquals(CallState.CALL_ENDED, focus.getCall().getCallState());
@@ -302,10 +300,10 @@ public void testFocusLeftTheRoomWithResume()
302300
// Create incoming call
303301
MockCall sipCall = sipProvider.getTelephony().mockIncomingGatewayCall("calee", roomName);
304302

305-
callStateWatch.waitForState(sipCall, CallState.CALL_IN_PROGRESS, 2000);
303+
callStateWatch.waitForState(sipCall, CallState.CALL_IN_PROGRESS);
306304

307305
// Now we expect SIP call to be in progress, but xmpp call ended
308-
callStateWatch.waitForState(focus.getCall(), CallState.CALL_ENDED, 2000);
306+
callStateWatch.waitForState(focus.getCall(), CallState.CALL_ENDED);
309307
assertNull(focus.getChatRoom());
310308

311309
AbstractGateway.setJvbInviteTimeout(origValue);
@@ -343,11 +341,8 @@ public void testCallControl()
343341
CallContext ctx = new CallContext(this);
344342
ctx.setDomain(serverName);
345343

346-
org.jivesoftware.smack.packet.IQ result = callControl.handleDialIq(dialIq, ctx, null);
347-
348-
assertNotNull(result);
349-
350-
RefIq callRef = (RefIq) result;
344+
var callRef = callControl.handleDialIq(dialIq, ctx, null);
345+
assertNotNull(callRef);
351346

352347
String callUri = callRef.getUri();
353348
assertEquals("xmpp:", callUri.substring(0, 5));
@@ -364,7 +359,7 @@ public void testCallControl()
364359

365360
CallStateListener callStateWatch = new CallStateListener();
366361

367-
callStateWatch.waitForState(sipCall, CallState.CALL_IN_PROGRESS, 1000);
362+
callStateWatch.waitForState(sipCall, CallState.CALL_IN_PROGRESS);
368363

369364
Jid callResource = JidCreate.from(callUri.substring(5)); //remove xmpp:
370365

@@ -377,7 +372,7 @@ public void testCallControl()
377372
Call xmppCall = session.getJvbCall();
378373

379374
// We joined JVB conference call
380-
callStateWatch.waitForState(xmppCall, CallState.CALL_IN_PROGRESS, 1000);
375+
callStateWatch.waitForState(xmppCall, CallState.CALL_IN_PROGRESS);
381376

382377
ChatRoom conferenceChatRoom = session.getJvbChatRoom();
383378

@@ -389,8 +384,8 @@ public void testCallControl()
389384
// FIXME: validate result
390385
callControl.handleHangUp(hangUp);
391386

392-
callStateWatch.waitForState(xmppCall, CallState.CALL_ENDED, 1000);
393-
callStateWatch.waitForState(sipCall, CallState.CALL_ENDED, 1000);
387+
callStateWatch.waitForState(xmppCall, CallState.CALL_ENDED);
388+
callStateWatch.waitForState(sipCall, CallState.CALL_ENDED);
394389
assertFalse(conferenceChatRoom.isJoined());
395390
}
396391

@@ -419,7 +414,7 @@ public void testDefaultJVbRoomProperty()
419414

420415
// SipGateway ought to accept incoming sip call
421416
// once JVB conference is joined.
422-
callStateWatch.waitForState(sipCall, CallState.CALL_IN_PROGRESS, 2000);
417+
callStateWatch.waitForState(sipCall, CallState.CALL_IN_PROGRESS);
423418

424419
// Now tear down, SIP calee ends the call
425420
// then XMPP cal should be terminated and MUC room left
@@ -435,8 +430,8 @@ public void testDefaultJVbRoomProperty()
435430

436431
CallManager.hangupCall(sipCall);
437432

438-
callStateWatch.waitForState(xmppCall, CallState.CALL_ENDED, 1000);
439-
callStateWatch.waitForState(sipCall, CallState.CALL_ENDED, 1000);
433+
callStateWatch.waitForState(xmppCall, CallState.CALL_ENDED);
434+
callStateWatch.waitForState(sipCall, CallState.CALL_ENDED);
440435
assertFalse(jvbRoom.isJoined());
441436
}
442437

@@ -458,9 +453,9 @@ public void testSimultaneousCalls()
458453

459454
CallStateListener callStateWatch = new CallStateListener();
460455

461-
callStateWatch.waitForState(sipCall1, CallState.CALL_IN_PROGRESS, 1000);
462-
callStateWatch.waitForState(sipCall2, CallState.CALL_IN_PROGRESS, 1000);
463-
callStateWatch.waitForState(sipCall3, CallState.CALL_IN_PROGRESS, 1000);
456+
callStateWatch.waitForState(sipCall1, CallState.CALL_IN_PROGRESS);
457+
callStateWatch.waitForState(sipCall2, CallState.CALL_IN_PROGRESS);
458+
callStateWatch.waitForState(sipCall3, CallState.CALL_IN_PROGRESS);
464459

465460
// Check peers are not on hold
466461
CallPeerStateListener peerStateWatch = new CallPeerStateListener();
@@ -492,9 +487,9 @@ public void testSimultaneousCalls()
492487
CallManager.hangupCall(sipCall2);
493488
CallManager.hangupCall(sipCall3);
494489

495-
callStateWatch.waitForState(jvbCall1, CallState.CALL_ENDED, 1000);
496-
callStateWatch.waitForState(jvbCall2, CallState.CALL_ENDED, 1000);
497-
callStateWatch.waitForState(jvbCall3, CallState.CALL_ENDED, 1000);
490+
callStateWatch.waitForState(jvbCall1, CallState.CALL_ENDED);
491+
callStateWatch.waitForState(jvbCall2, CallState.CALL_ENDED);
492+
callStateWatch.waitForState(jvbCall3, CallState.CALL_ENDED);
498493

499494
assertEquals(CallState.CALL_ENDED, sipCall1.getCallState());
500495
assertEquals(CallState.CALL_ENDED, sipCall2.getCallState());
@@ -530,7 +525,7 @@ public void testNoFocusInTheRoom()
530525

531526
// Assert incoming call state
532527
callStateWatch.waitForState(
533-
sipCall1, CallState.CALL_INITIALIZATION, 1000);
528+
sipCall1, CallState.CALL_INITIALIZATION);
534529

535530
// We expect to have 1 active sessions
536531
List<SipGatewaySession> sessions = gatewaySessions.getSessions(1000);
@@ -551,7 +546,7 @@ public void testNoFocusInTheRoom()
551546
assertNull(jvbCall1);
552547

553548
callStateWatch.waitForState(
554-
sipCall1, CallState.CALL_ENDED, jvbInviteTimeout + 200);
549+
sipCall1, CallState.CALL_ENDED);
555550

556551
assertFalse(jvbRoom1.isJoined());
557552

src/test/java/org/jitsi/jigasi/MockJvbConferenceFocus.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,8 @@ public void serviceChanged(ServiceEvent serviceEvent)
8787

8888
setXmppProvider(protocol);
8989
}
90-
catch (OperationFailedException e)
91-
{
92-
logger.error(e, e);
93-
}
94-
catch (OperationNotSupportedException e)
90+
catch (OperationFailedException |
91+
OperationNotSupportedException e)
9592
{
9693
logger.error(e, e);
9794
}
@@ -143,15 +140,7 @@ private void inviteToConference(ChatRoomMember member)
143140
if (leaveRoomAfterInvite)
144141
{
145142
logger.info(myName + " invited peer will leave the room");
146-
new Thread(new Runnable()
147-
{
148-
@Override
149-
public void run()
150-
{
151-
logger.info(myName + " leaving the room");
152-
tearDown();
153-
}
154-
}).start();
143+
tearDown();
155144
}
156145
}
157146

0 commit comments

Comments
 (0)