feat(transcode): lock the destination transcode cache path

This commit is contained in:
sentriz
2023-10-09 20:43:51 +01:00
parent 7eaf602e69
commit c9a2d2f9ce
2 changed files with 60 additions and 2 deletions

View File

@@ -2,11 +2,14 @@ package transcode_test
import (
"bytes"
"context"
"io"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"sync"
"sync/atomic"
"testing"
"time"
@@ -100,3 +103,41 @@ func TestTranscodeWithSeek(t *testing.T) {
// since we seeked 2 seconds, we should have 5-2 = 3 seconds of PCM data
require.Equal(t, (testFileLen-seekSecs)*bytesPerSec, buf.Len())
}
func TestCachingParallelism(t *testing.T) {
t.Parallel()
var realTranscodeCount atomic.Uint64
transcoder := callbackTranscoder{
transcoder: transcode.NewFFmpegTranscoder(),
callback: func() { realTranscodeCount.Add(1) },
}
cacheTranscoder := transcode.NewCachingTranscoder(transcoder, t.TempDir())
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
var buf bytes.Buffer
require.NoError(t, cacheTranscoder.Transcode(context.Background(), transcode.PCM16le, "testdata/5s.flac", &buf))
require.Equal(t, 5*bytesPerSec, buf.Len())
}()
}
wg.Wait()
require.Equal(t, 1, int(realTranscodeCount.Load()))
}
type callbackTranscoder struct {
transcoder transcode.Transcoder
callback func()
}
func (ct callbackTranscoder) Transcode(ctx context.Context, profile transcode.Profile, in string, out io.Writer) error {
ct.callback()
return ct.transcoder.Transcode(ctx, profile, in, out)
}