Skip to content

Commit

Permalink
fixup! core: reuse remote transcoder for stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuben Rodrigues committed May 4, 2021
1 parent afb9ab6 commit e5654d8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
66 changes: 38 additions & 28 deletions core/orch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math/big"
"math/rand"
"os"
"strconv"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions core/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e5654d8

Please sign in to comment.