2020-04-04 18:45:29 +00:00
|
|
|
package filecache_test
|
|
|
|
|
|
|
|
import (
|
2020-04-04 20:11:21 +00:00
|
|
|
"bytes"
|
2020-04-04 18:45:29 +00:00
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2024-06-05 17:36:34 +00:00
|
|
|
"os"
|
2020-04-04 18:45:29 +00:00
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
|
2024-06-05 17:36:34 +00:00
|
|
|
"gitlab.com/slon/shad-go/distbuild/pkg/build"
|
|
|
|
"gitlab.com/slon/shad-go/distbuild/pkg/filecache"
|
2020-04-04 18:45:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type env struct {
|
|
|
|
cache *testCache
|
|
|
|
server *httptest.Server
|
|
|
|
client *filecache.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEnv(t *testing.T) *env {
|
|
|
|
l := zaptest.NewLogger(t)
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
cache := newCache(t)
|
|
|
|
|
|
|
|
handler := filecache.NewHandler(l, cache.Cache)
|
|
|
|
handler.Register(mux)
|
|
|
|
|
|
|
|
server := httptest.NewServer(mux)
|
2023-04-26 11:51:11 +00:00
|
|
|
t.Cleanup(server.Close)
|
2020-04-04 18:45:29 +00:00
|
|
|
|
|
|
|
client := filecache.NewClient(l, server.URL)
|
|
|
|
|
|
|
|
env := &env{
|
|
|
|
cache: cache,
|
|
|
|
server: server,
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileUpload(t *testing.T) {
|
|
|
|
env := newEnv(t)
|
2020-04-04 20:11:21 +00:00
|
|
|
content := bytes.Repeat([]byte("foobar"), 1024*1024)
|
|
|
|
|
2020-04-04 18:45:29 +00:00
|
|
|
tmpFilePath := filepath.Join(env.cache.tmpDir, "foo.txt")
|
2024-06-05 17:36:34 +00:00
|
|
|
require.NoError(t, os.WriteFile(tmpFilePath, content, 0666))
|
2020-04-04 18:45:29 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
t.Run("UploadSingleFile", func(t *testing.T) {
|
|
|
|
id := build.ID{0x01}
|
|
|
|
|
|
|
|
require.NoError(t, env.client.Upload(ctx, id, tmpFilePath))
|
|
|
|
|
|
|
|
path, unlock, err := env.cache.Get(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer unlock()
|
|
|
|
|
2024-06-05 17:36:34 +00:00
|
|
|
actualContent, err := os.ReadFile(path)
|
2020-04-04 18:45:29 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-22 23:23:39 +00:00
|
|
|
require.Equal(t, content, actualContent)
|
2020-04-04 18:45:29 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("RepeatedUpload", func(t *testing.T) {
|
|
|
|
id := build.ID{0x02}
|
|
|
|
|
|
|
|
require.NoError(t, env.client.Upload(ctx, id, tmpFilePath))
|
|
|
|
require.NoError(t, env.client.Upload(ctx, id, tmpFilePath))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ConcurrentUpload", func(t *testing.T) {
|
|
|
|
const (
|
2020-04-04 20:11:21 +00:00
|
|
|
N = 10
|
|
|
|
G = 10
|
2020-04-04 18:45:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(G)
|
|
|
|
|
2020-04-04 21:55:07 +00:00
|
|
|
id := build.ID{0x03, byte(i)}
|
2020-04-04 18:45:29 +00:00
|
|
|
for j := 0; j < G; j++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
assert.NoError(t, env.client.Upload(ctx, id, tmpFilePath))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileDownload(t *testing.T) {
|
|
|
|
env := newEnv(t)
|
|
|
|
|
|
|
|
localCache := newCache(t)
|
|
|
|
|
|
|
|
id := build.ID{0x01}
|
|
|
|
|
|
|
|
w, abort, err := env.cache.Write(id)
|
|
|
|
require.NoError(t, err)
|
2020-04-04 21:55:07 +00:00
|
|
|
defer func() { _ = abort() }()
|
2020-04-04 18:45:29 +00:00
|
|
|
|
|
|
|
_, err = w.Write([]byte("foobar"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, w.Close())
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
require.NoError(t, env.client.Download(ctx, localCache.Cache, id))
|
|
|
|
|
|
|
|
path, unlock, err := localCache.Get(id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer unlock()
|
|
|
|
|
2024-06-05 17:36:34 +00:00
|
|
|
content, err := os.ReadFile(path)
|
2020-04-04 18:45:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []byte("foobar"), content)
|
|
|
|
}
|