2023-07-05 15:23:50 +00:00
|
|
|
package ocean_test
|
|
|
|
|
|
|
|
import (
|
2023-07-06 17:40:33 +00:00
|
|
|
"sync"
|
2023-07-05 15:23:50 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.akyoto.dev/go/assert"
|
|
|
|
"git.akyoto.dev/go/ocean"
|
|
|
|
)
|
|
|
|
|
2023-07-06 12:10:12 +00:00
|
|
|
type User struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
2023-07-08 10:13:29 +00:00
|
|
|
func TestCollection(t *testing.T) {
|
2023-07-10 14:27:41 +00:00
|
|
|
users, err := ocean.New[User]("test", nil)
|
2023-07-05 21:59:49 +00:00
|
|
|
assert.Nil(t, err)
|
2023-07-08 20:10:02 +00:00
|
|
|
|
2023-07-08 15:26:36 +00:00
|
|
|
defer users.Sync()
|
2023-07-06 12:10:12 +00:00
|
|
|
defer users.Clear()
|
2023-07-05 21:59:49 +00:00
|
|
|
|
2023-07-06 12:10:12 +00:00
|
|
|
users.Set("1", &User{Name: "User 1"})
|
2023-07-06 15:50:48 +00:00
|
|
|
users.Set("2", &User{Name: "User 2"})
|
2023-07-06 21:46:47 +00:00
|
|
|
users.Set("3", &User{Name: "User 3"})
|
2023-07-05 15:23:50 +00:00
|
|
|
|
2023-07-08 10:13:29 +00:00
|
|
|
t.Run("All", func(t *testing.T) {
|
|
|
|
count := 0
|
|
|
|
|
|
|
|
for range users.All() {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, count, 3)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Filter", func(t *testing.T) {
|
|
|
|
count := 0
|
|
|
|
|
|
|
|
f := func(user *User) bool {
|
|
|
|
return user.Name == "User 2"
|
|
|
|
}
|
|
|
|
|
|
|
|
for range users.Filter(f) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, count, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Get", func(t *testing.T) {
|
|
|
|
user, err := users.Get("1")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, user)
|
|
|
|
assert.Equal(t, user.Name, "User 1")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Exists", func(t *testing.T) {
|
|
|
|
assert.True(t, users.Exists("1"))
|
|
|
|
assert.True(t, users.Exists("2"))
|
|
|
|
assert.True(t, users.Exists("3"))
|
|
|
|
assert.True(t, !users.Exists("4"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Parallel Get and Set", func(t *testing.T) {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
user, err := users.Get("1")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, user)
|
|
|
|
assert.Equal(t, user.Name, "User 1")
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
users.Set("1", &User{Name: "User 1"})
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Delete", func(t *testing.T) {
|
|
|
|
assert.True(t, users.Exists("1"))
|
|
|
|
assert.True(t, users.Exists("2"))
|
|
|
|
assert.True(t, users.Exists("3"))
|
|
|
|
|
|
|
|
users.Delete("1")
|
|
|
|
|
|
|
|
assert.True(t, !users.Exists("1"))
|
|
|
|
assert.True(t, users.Exists("2"))
|
|
|
|
assert.True(t, users.Exists("3"))
|
|
|
|
|
|
|
|
users.Delete("2")
|
|
|
|
|
|
|
|
assert.True(t, !users.Exists("1"))
|
|
|
|
assert.True(t, !users.Exists("2"))
|
|
|
|
assert.True(t, users.Exists("3"))
|
|
|
|
|
|
|
|
users.Delete("3")
|
|
|
|
|
|
|
|
assert.True(t, !users.Exists("1"))
|
|
|
|
assert.True(t, !users.Exists("2"))
|
|
|
|
assert.True(t, !users.Exists("3"))
|
|
|
|
})
|
2023-07-06 17:40:33 +00:00
|
|
|
}
|