From e5654d807f84faad8c6e87225d65494f0172608a Mon Sep 17 00:00:00 2001 From: Reuben Rodrigues Date: Tue, 4 May 2021 13:36:17 +0000 Subject: [PATCH] fixup! core: reuse remote transcoder for stream --- core/orch_test.go | 66 +++++++++++++++++++++++++------------------- core/orchestrator.go | 5 ++++ 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/core/orch_test.go b/core/orch_test.go index bb8115ec8f..67fe910c9e 100644 --- a/core/orch_test.go +++ b/core/orch_test.go @@ -9,6 +9,7 @@ import ( "math/big" "math/rand" "os" + "strconv" "sync" "testing" "time" @@ -388,36 +389,45 @@ func TestCompleteStreamSession(t *testing.T) { } func TestRemoveFromRemoteTranscoders(t *testing.T) { - m := NewRemoteTranscoderManager() - strm := &StubTranscoderServer{manager: m} - strm2 := &StubTranscoderServer{manager: m} - - // sanity check that transcoder is not in liveTranscoders or remoteTranscoders + remoteTranscoderList := []*RemoteTranscoder{} assert := assert.New(t) - assert.Nil(m.liveTranscoders[strm]) - assert.Empty(m.remoteTranscoders) - - // register transcoders, which adds transcoder to liveTranscoders and remoteTranscoders - go func() { m.Manage(strm, 1) }() - time.Sleep(1 * time.Millisecond) // allow time for first stream to register - go func() { m.Manage(strm2, 1) }() - time.Sleep(1 * time.Millisecond) // allow time for second stream to register - - // assert two transcoders added - assert.Len(m.remoteTranscoders, 2) - t1 := m.liveTranscoders[strm] - t2 := m.liveTranscoders[strm2] - - assert.Equal(m.remoteTranscoders[0], t1) - assert.Equal(m.remoteTranscoders[1], t2) - - // Remove transcoders and assert size reduces - m.remoteTranscoders = removeFromRemoteTranscoders(t1, m.remoteTranscoders) - assert.Equal(m.remoteTranscoders[0], t2) - assert.Len(m.remoteTranscoders, 1) - m.remoteTranscoders = removeFromRemoteTranscoders(t2, m.remoteTranscoders) - assert.Len(m.remoteTranscoders, 0) + // Create 4 tanscoders + tr := make([]*RemoteTranscoder, 4) + for i := 0; i < 4; i++ { + tr[i] = &RemoteTranscoder{addr: "testAddress" + strconv.Itoa(i)} + } + + // Add to list + remoteTranscoderList = append(remoteTranscoderList, tr...) + assert.Len(remoteTranscoderList, 4) + + // Remove transcoder froms head of the list + remoteTranscoderList = removeFromRemoteTranscoders(tr[0], remoteTranscoderList) + assert.Equal(remoteTranscoderList[0], tr[1]) + assert.Equal(remoteTranscoderList[1], tr[2]) + assert.Equal(remoteTranscoderList[2], tr[3]) + assert.Len(remoteTranscoderList, 3) + + // Remove transcoder from the middle of the list + remoteTranscoderList = removeFromRemoteTranscoders(tr[3], remoteTranscoderList) + assert.Equal(remoteTranscoderList[0], tr[1]) + assert.Equal(remoteTranscoderList[1], tr[2]) + assert.Len(remoteTranscoderList, 2) + + // Remove transcoder from the end of the list + remoteTranscoderList = removeFromRemoteTranscoders(tr[2], remoteTranscoderList) + assert.Equal(remoteTranscoderList[0], tr[1]) + assert.Len(remoteTranscoderList, 1) + + // Remove the last transcoder + remoteTranscoderList = removeFromRemoteTranscoders(tr[1], remoteTranscoderList) + assert.Len(remoteTranscoderList, 0) + + // Remove a transcoder when list is empty + remoteTranscoderList = removeFromRemoteTranscoders(tr[1], remoteTranscoderList) + emptyTList := []*RemoteTranscoder{} + assert.Equal(remoteTranscoderList, emptyTList) } func TestTranscoderManagerTranscoding(t *testing.T) { diff --git a/core/orchestrator.go b/core/orchestrator.go index 8d68af24e4..ad475e4811 100644 --- a/core/orchestrator.go +++ b/core/orchestrator.go @@ -860,6 +860,11 @@ func (rtm *RemoteTranscoderManager) Manage(stream net.Transcoder_RegisterTransco } func removeFromRemoteTranscoders(rt *RemoteTranscoder, remoteTranscoders []*RemoteTranscoder) []*RemoteTranscoder { + if len(remoteTranscoders) == 0 { + // No transocerds to remove, return + return remoteTranscoders + } + lastIndex := len(remoteTranscoders) - 1 last := remoteTranscoders[lastIndex] if rt == last {