datarhei-dragonfork-core/restream/fs/fs_test.go

169 lines
3.5 KiB
Go
Raw Normal View History

2022-05-13 13:26:45 -04:00
package fs
import (
"strings"
"testing"
"time"
"github.com/datarhei/core/v16/io/fs"
2022-05-13 13:26:45 -04:00
"github.com/stretchr/testify/require"
)
func TestMaxFiles(t *testing.T) {
2023-02-01 10:09:20 -05:00
memfs, _ := fs.NewMemFilesystem(fs.MemConfig{})
2022-05-13 13:26:45 -04:00
cleanfs := New(Config{
FS: memfs,
})
cleanfs.Start()
cleanfs.SetCleanup("foobar", []Pattern{
{
Pattern: "/*.ts",
MaxFiles: 3,
MaxFileAge: 0,
},
})
2023-02-01 10:09:20 -05:00
cleanfs.WriteFileReader("/chunk_0.ts", strings.NewReader("chunk_0"))
cleanfs.WriteFileReader("/chunk_1.ts", strings.NewReader("chunk_1"))
cleanfs.WriteFileReader("/chunk_2.ts", strings.NewReader("chunk_2"))
2022-05-13 13:26:45 -04:00
require.Eventually(t, func() bool {
return cleanfs.Files() == 3
}, 3*time.Second, time.Second)
2023-02-01 10:09:20 -05:00
cleanfs.WriteFileReader("/chunk_3.ts", strings.NewReader("chunk_3"))
2022-05-13 13:26:45 -04:00
require.Eventually(t, func() bool {
if cleanfs.Files() != 3 {
return false
}
names := []string{}
2023-02-01 10:09:20 -05:00
for _, f := range cleanfs.List("/", "/*.ts") {
2022-05-13 13:26:45 -04:00
names = append(names, f.Name())
}
require.ElementsMatch(t, []string{"/chunk_1.ts", "/chunk_2.ts", "/chunk_3.ts"}, names)
return true
}, 3*time.Second, time.Second)
cleanfs.Stop()
}
func TestMaxAge(t *testing.T) {
2023-02-01 10:09:20 -05:00
memfs, _ := fs.NewMemFilesystem(fs.MemConfig{})
2022-05-13 13:26:45 -04:00
cleanfs := New(Config{
FS: memfs,
})
cleanfs.Start()
cleanfs.SetCleanup("foobar", []Pattern{
{
Pattern: "/*.ts",
MaxFiles: 0,
MaxFileAge: 3 * time.Second,
},
})
2023-02-01 10:09:20 -05:00
cleanfs.WriteFileReader("/chunk_0.ts", strings.NewReader("chunk_0"))
cleanfs.WriteFileReader("/chunk_1.ts", strings.NewReader("chunk_1"))
cleanfs.WriteFileReader("/chunk_2.ts", strings.NewReader("chunk_2"))
2022-05-13 13:26:45 -04:00
require.Eventually(t, func() bool {
return cleanfs.Files() == 0
2023-04-26 03:50:09 -04:00
}, 10*time.Second, time.Second)
2022-05-13 13:26:45 -04:00
2023-02-01 10:09:20 -05:00
cleanfs.WriteFileReader("/chunk_3.ts", strings.NewReader("chunk_3"))
2022-05-13 13:26:45 -04:00
require.Eventually(t, func() bool {
if cleanfs.Files() != 1 {
return false
}
names := []string{}
2023-02-01 10:09:20 -05:00
for _, f := range cleanfs.List("/", "/*.ts") {
2022-05-13 13:26:45 -04:00
names = append(names, f.Name())
}
require.ElementsMatch(t, []string{"/chunk_3.ts"}, names)
return true
2023-04-26 03:50:09 -04:00
}, 5*time.Second, time.Second)
2022-05-13 13:26:45 -04:00
cleanfs.Stop()
}
func TestUnsetCleanup(t *testing.T) {
2023-02-01 10:09:20 -05:00
memfs, _ := fs.NewMemFilesystem(fs.MemConfig{})
2022-05-13 13:26:45 -04:00
cleanfs := New(Config{
FS: memfs,
})
cleanfs.Start()
cleanfs.SetCleanup("foobar", []Pattern{
{
Pattern: "/*.ts",
MaxFiles: 3,
MaxFileAge: 0,
},
})
2023-02-01 10:09:20 -05:00
cleanfs.WriteFileReader("/chunk_0.ts", strings.NewReader("chunk_0"))
cleanfs.WriteFileReader("/chunk_1.ts", strings.NewReader("chunk_1"))
cleanfs.WriteFileReader("/chunk_2.ts", strings.NewReader("chunk_2"))
2022-05-13 13:26:45 -04:00
require.Eventually(t, func() bool {
return cleanfs.Files() == 3
}, 3*time.Second, time.Second)
2023-02-01 10:09:20 -05:00
cleanfs.WriteFileReader("/chunk_3.ts", strings.NewReader("chunk_3"))
2022-05-13 13:26:45 -04:00
require.Eventually(t, func() bool {
if cleanfs.Files() != 3 {
return false
}
names := []string{}
2023-02-01 10:09:20 -05:00
for _, f := range cleanfs.List("/", "/*.ts") {
2022-05-13 13:26:45 -04:00
names = append(names, f.Name())
}
require.ElementsMatch(t, []string{"/chunk_1.ts", "/chunk_2.ts", "/chunk_3.ts"}, names)
return true
}, 3*time.Second, time.Second)
cleanfs.UnsetCleanup("foobar")
2023-02-01 10:09:20 -05:00
cleanfs.WriteFileReader("/chunk_4.ts", strings.NewReader("chunk_4"))
2022-05-13 13:26:45 -04:00
require.Eventually(t, func() bool {
if cleanfs.Files() != 4 {
return false
}
names := []string{}
2023-02-01 10:09:20 -05:00
for _, f := range cleanfs.List("/", "/*.ts") {
2022-05-13 13:26:45 -04:00
names = append(names, f.Name())
}
require.ElementsMatch(t, []string{"/chunk_1.ts", "/chunk_2.ts", "/chunk_3.ts", "/chunk_4.ts"}, names)
return true
}, 3*time.Second, time.Second)
cleanfs.Stop()
}