2019-12-16 18:09:59 +03:00
|
|
|
package cockroach
|
2019-11-01 17:13:21 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kr/pretty"
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/store"
|
2019-11-01 17:13:21 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSQL(t *testing.T) {
|
|
|
|
connection := fmt.Sprintf(
|
|
|
|
"host=%s port=%d user=%s sslmode=disable dbname=%s",
|
|
|
|
"localhost",
|
2020-03-17 19:15:23 +03:00
|
|
|
26257,
|
|
|
|
"root",
|
2019-11-01 17:13:21 +03:00
|
|
|
"test",
|
|
|
|
)
|
|
|
|
db, err := sql.Open("postgres", connection)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := db.Ping(); err != nil {
|
2020-04-09 14:58:50 +03:00
|
|
|
t.Skip("store/cockroach: can't connect to db")
|
2019-11-01 17:13:21 +03:00
|
|
|
}
|
|
|
|
db.Close()
|
|
|
|
|
2020-01-06 20:44:32 +03:00
|
|
|
sqlStore := NewStore(
|
2020-04-06 18:45:55 +03:00
|
|
|
store.Database("testsql"),
|
2019-11-01 17:13:21 +03:00
|
|
|
store.Nodes(connection),
|
|
|
|
)
|
|
|
|
|
2020-03-17 19:15:23 +03:00
|
|
|
if err := sqlStore.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
keys, err := sqlStore.List()
|
2019-11-01 17:13:21 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
2020-03-12 16:41:30 +03:00
|
|
|
t.Logf("%# v\n", pretty.Formatter(keys))
|
2019-11-01 17:13:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = sqlStore.Write(
|
|
|
|
&store.Record{
|
|
|
|
Key: "test",
|
|
|
|
Value: []byte("foo"),
|
|
|
|
},
|
2020-01-09 01:23:14 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = sqlStore.Write(
|
2019-11-01 17:13:21 +03:00
|
|
|
&store.Record{
|
|
|
|
Key: "bar",
|
|
|
|
Value: []byte("baz"),
|
|
|
|
},
|
2020-01-09 01:23:14 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = sqlStore.Write(
|
2019-11-01 17:13:21 +03:00
|
|
|
&store.Record{
|
|
|
|
Key: "qux",
|
|
|
|
Value: []byte("aasad"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = sqlStore.Delete("qux")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sqlStore.Write(&store.Record{
|
|
|
|
Key: "test",
|
|
|
|
Value: []byte("bar"),
|
2020-03-17 19:15:23 +03:00
|
|
|
Expiry: time.Second * 10,
|
2019-11-01 17:13:21 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:30 +03:00
|
|
|
records, err := sqlStore.Read("test")
|
2019-11-01 17:13:21 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2019-12-03 22:59:44 +03:00
|
|
|
}
|
|
|
|
t.Logf("%# v\n", pretty.Formatter(records))
|
|
|
|
if string(records[0].Value) != "bar" {
|
|
|
|
t.Error("Expected bar, got ", string(records[0].Value))
|
2019-11-01 17:13:21 +03:00
|
|
|
}
|
|
|
|
|
2020-03-17 19:15:23 +03:00
|
|
|
time.Sleep(11 * time.Second)
|
2019-12-03 22:59:44 +03:00
|
|
|
_, err = sqlStore.Read("test")
|
|
|
|
switch err {
|
|
|
|
case nil:
|
2019-11-01 17:13:21 +03:00
|
|
|
t.Error("Key test should have expired")
|
2019-12-03 22:59:44 +03:00
|
|
|
default:
|
|
|
|
t.Error(err)
|
|
|
|
case store.ErrNotFound:
|
|
|
|
break
|
2019-11-01 17:13:21 +03:00
|
|
|
}
|
2020-03-17 19:15:23 +03:00
|
|
|
sqlStore.Delete("bar")
|
2020-08-18 20:30:05 +03:00
|
|
|
sqlStore.Write(&store.Record{Key: "aaa", Value: []byte("bbb"), Expiry: 10 * time.Second})
|
|
|
|
sqlStore.Write(&store.Record{Key: "aaaa", Value: []byte("bbb"), Expiry: 10 * time.Second})
|
|
|
|
sqlStore.Write(&store.Record{Key: "aaaaa", Value: []byte("bbb"), Expiry: 10 * time.Second})
|
2020-03-17 19:15:23 +03:00
|
|
|
results, err := sqlStore.Read("a", store.ReadPrefix())
|
2020-03-27 13:15:37 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2020-03-17 19:15:23 +03:00
|
|
|
if len(results) != 3 {
|
|
|
|
t.Fatal("Results should have returned 3 records")
|
|
|
|
}
|
2020-08-18 20:30:05 +03:00
|
|
|
time.Sleep(10 * time.Second)
|
2020-03-17 19:15:23 +03:00
|
|
|
results, err = sqlStore.Read("a", store.ReadPrefix())
|
2020-03-27 13:15:37 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2020-03-17 19:15:23 +03:00
|
|
|
if len(results) != 0 {
|
|
|
|
t.Fatal("Results should have returned 0 records")
|
|
|
|
}
|
2020-08-18 20:30:05 +03:00
|
|
|
|
|
|
|
sqlStore.Write(&store.Record{Key: "bbb", Value: []byte("bbb")}, store.WriteExpiry(time.Now().Add(10*time.Second)))
|
|
|
|
sqlStore.Write(&store.Record{Key: "bbbb", Value: []byte("bbb")}, store.WriteExpiry(time.Now().Add(10*time.Second)))
|
|
|
|
sqlStore.Write(&store.Record{Key: "bbbbb", Value: []byte("bbb")}, store.WriteExpiry(time.Now().Add(10*time.Second)))
|
|
|
|
results, err = sqlStore.Read("b", store.ReadPrefix())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if len(results) != 3 {
|
|
|
|
t.Fatalf("Results should have returned 3 records. Received %d", len(results))
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
results, err = sqlStore.Read("b", store.ReadPrefix())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if len(results) != 0 {
|
|
|
|
t.Fatalf("Results should have returned 0 records. Received %d", len(results))
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlStore.Write(&store.Record{Key: "ccc", Value: []byte("bbb")}, store.WriteTTL(10*time.Second))
|
|
|
|
sqlStore.Write(&store.Record{Key: "cccc", Value: []byte("bbb")}, store.WriteTTL(10*time.Second))
|
|
|
|
sqlStore.Write(&store.Record{Key: "ccccc", Value: []byte("bbb")}, store.WriteTTL(10*time.Second))
|
|
|
|
results, err = sqlStore.Read("c", store.ReadPrefix())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if len(results) != 3 {
|
|
|
|
t.Fatalf("Results should have returned 3 records. Received %d", len(results))
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
results, err = sqlStore.Read("c", store.ReadPrefix())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if len(results) != 0 {
|
|
|
|
t.Fatalf("Results should have returned 0 records. Received %d", len(results))
|
|
|
|
}
|
2019-11-01 17:13:21 +03:00
|
|
|
}
|