2021-11-16 04:56:33 +00:00
|
|
|
package cheapcash_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
2021-11-16 13:18:23 +00:00
|
|
|
"sync"
|
2021-11-16 04:56:33 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aldy505/cheapcash"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExists(t *testing.T) {
|
2021-11-16 13:18:23 +00:00
|
|
|
randomValue := strconv.Itoa(rand.Int())
|
2021-11-16 04:56:33 +00:00
|
|
|
c := cheapcash.Default()
|
2021-11-16 13:18:23 +00:00
|
|
|
b, err := c.Exists(c.Path + "/" + randomValue)
|
2021-11-16 04:56:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b == true {
|
|
|
|
t.Error("expected false, got true")
|
|
|
|
}
|
|
|
|
|
2021-11-16 13:18:23 +00:00
|
|
|
err = c.Write(randomValue, []byte("value"))
|
2021-11-16 04:56:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
2021-11-16 13:18:23 +00:00
|
|
|
b, err = c.Exists(c.Path + "/" + randomValue)
|
2021-11-16 04:56:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b == false {
|
|
|
|
t.Error("expected true, got false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 05:01:41 +00:00
|
|
|
func TestExists_Concurrency(t *testing.T) {
|
2021-11-16 13:18:23 +00:00
|
|
|
randomValue := strconv.Itoa(rand.Int())
|
2021-11-16 04:56:33 +00:00
|
|
|
c := cheapcash.Default()
|
|
|
|
|
2021-11-16 13:18:23 +00:00
|
|
|
var wg sync.WaitGroup
|
2021-11-16 04:56:33 +00:00
|
|
|
|
2021-11-16 13:18:23 +00:00
|
|
|
existsFunc := func() {
|
|
|
|
b, err := c.Exists(c.Path + "/" + randomValue)
|
2021-11-16 04:56:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
if b == true {
|
|
|
|
t.Error("expected false, got true")
|
|
|
|
}
|
2021-11-16 13:18:23 +00:00
|
|
|
wg.Done()
|
|
|
|
}
|
2021-11-16 04:56:33 +00:00
|
|
|
|
2021-11-16 13:18:23 +00:00
|
|
|
wg.Add(5)
|
|
|
|
go existsFunc()
|
|
|
|
go existsFunc()
|
|
|
|
go existsFunc()
|
|
|
|
go existsFunc()
|
|
|
|
go existsFunc()
|
2021-11-16 04:56:33 +00:00
|
|
|
|
2021-11-16 13:18:23 +00:00
|
|
|
wg.Wait()
|
2021-11-16 04:56:33 +00:00
|
|
|
}
|