|
| 1 | +package org.jitsi.jicofo.recording.jibri |
| 2 | + |
| 3 | +import io.kotest.assertions.throwables.shouldThrow |
| 4 | +import io.kotest.core.spec.IsolationMode |
| 5 | +import io.kotest.core.spec.style.ShouldSpec |
| 6 | +import io.kotest.matchers.collections.shouldHaveSize |
| 7 | +import io.kotest.matchers.shouldNotBe |
| 8 | +import io.mockk.every |
| 9 | +import io.mockk.mockk |
| 10 | +import io.mockk.mockkStatic |
| 11 | +import io.mockk.spyk |
| 12 | +import io.mockk.verify |
| 13 | +import org.jitsi.eventadmin.EventAdmin |
| 14 | +import org.jitsi.impl.osgi.framework.BundleContextImpl |
| 15 | +import org.jitsi.jicofo.FocusBundleActivator |
| 16 | +import org.jitsi.osgi.ServiceUtils2 |
| 17 | +import org.jitsi.protocol.xmpp.XmppConnection |
| 18 | +import org.jitsi.test.concurrent.FakeScheduledExecutorService |
| 19 | +import org.jitsi.utils.logging.Logger |
| 20 | +import org.jitsi.xmpp.extensions.jibri.JibriIq |
| 21 | +import org.jivesoftware.smack.packet.IQ |
| 22 | +import org.jivesoftware.smack.packet.XMPPError |
| 23 | +import org.jxmpp.jid.impl.JidCreate |
| 24 | + |
| 25 | +class JibriSessionTest : ShouldSpec({ |
| 26 | + isolationMode = IsolationMode.InstancePerLeaf |
| 27 | + |
| 28 | + mockkStatic(ServiceUtils2::class) |
| 29 | + every { ServiceUtils2.getService(any(), EventAdmin::class.java)} returns mockk(relaxed = true) |
| 30 | + val bundleContext: BundleContextImpl = mockk(relaxed = true) |
| 31 | + val owner: JibriSession.Owner = mockk(relaxed = true) |
| 32 | + val roomName = JidCreate.entityBareFrom(" [email protected]/baz") |
| 33 | + val initiator = JidCreate.bareFrom(" [email protected]/baz") |
| 34 | + val pendingTimeout = 60L |
| 35 | + val maxNumRetries = 2 |
| 36 | + val xmppConnection: XmppConnection = mockk() |
| 37 | + val executor: FakeScheduledExecutorService = spyk() |
| 38 | + val jibriList = mutableListOf( |
| 39 | + JidCreate.bareFrom(" [email protected]"), |
| 40 | + JidCreate.bareFrom(" [email protected]"), |
| 41 | + JidCreate.bareFrom(" [email protected]") |
| 42 | + ) |
| 43 | + val detector: JibriDetector = mockk { |
| 44 | + every { selectJibri() } returnsMany(jibriList) |
| 45 | + every { isAnyInstanceConnected } returns true |
| 46 | + every { memberHadTransientError(any()) } answers { |
| 47 | + // Simulate the real JibriDetector logic and put the Jibri at the back of the list |
| 48 | + jibriList.remove(arg(0)) |
| 49 | + jibriList.add(arg(0)) |
| 50 | + } |
| 51 | + } |
| 52 | + val logger: Logger = mockk(relaxed = true) |
| 53 | + |
| 54 | + val jibriSession = JibriSession( |
| 55 | + bundleContext, |
| 56 | + owner, |
| 57 | + roomName, |
| 58 | + initiator, |
| 59 | + pendingTimeout, |
| 60 | + maxNumRetries, |
| 61 | + xmppConnection, |
| 62 | + executor, |
| 63 | + detector, |
| 64 | + false /* isSIP */, |
| 65 | + null /* sipAddress */, |
| 66 | + "displayName", |
| 67 | + "streamID", |
| 68 | + "youTubeBroadcastId", |
| 69 | + "sessionId", |
| 70 | + "applicationData", |
| 71 | + logger |
| 72 | + ) |
| 73 | + |
| 74 | + FocusBundleActivator.bundleContext = bundleContext |
| 75 | + |
| 76 | + context("When sending a request to a Jibri to start a session throws an error") { |
| 77 | + val iqRequests = mutableListOf<IQ>() |
| 78 | + every { xmppConnection.sendPacketAndGetReply(capture(iqRequests)) } answers { |
| 79 | + // First return error |
| 80 | + IQ.createErrorResponse(arg(0), XMPPError.Condition.service_unavailable) |
| 81 | + } andThen { |
| 82 | + // Then return a successful response |
| 83 | + JibriIq().apply { |
| 84 | + status = JibriIq.Status.PENDING |
| 85 | + from = (arg(0) as IQ).to |
| 86 | + } |
| 87 | + } |
| 88 | + context("Trying to start a Jibri session") { |
| 89 | + should("retry with another jibri") { |
| 90 | + jibriSession.start() |
| 91 | + verify(exactly = 2) { xmppConnection.sendPacketAndGetReply(any()) } |
| 92 | + iqRequests shouldHaveSize 2 |
| 93 | + iqRequests[0].to shouldNotBe iqRequests[1].to |
| 94 | + } |
| 95 | + } |
| 96 | + context("and that's the only Jibri") { |
| 97 | + every { detector.selectJibri() } returns JidCreate.bareFrom(" [email protected]") |
| 98 | + every { xmppConnection.sendPacketAndGetReply(capture(iqRequests)) } answers { |
| 99 | + // First return error |
| 100 | + IQ.createErrorResponse(arg(0), XMPPError.Condition.service_unavailable) |
| 101 | + } |
| 102 | + context("trying to start a jibri session") { |
| 103 | + should("give up after exceeding the retry count") { |
| 104 | + shouldThrow<JibriSession.StartException> { |
| 105 | + jibriSession.start() |
| 106 | + } |
| 107 | + verify(exactly = maxNumRetries + 1) { xmppConnection.sendPacketAndGetReply(any())} |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +}) |
0 commit comments