-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream_test.go
143 lines (113 loc) · 2.61 KB
/
stream_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package llsr
import (
"database/sql"
_ "github.com/lib/pq"
"os"
"testing"
"time"
"github.com/liquidm/llsr/decoderbufs"
)
type testStreamCallback func(*testing.T, *sql.DB, *Stream)
var user string
var dbname string
func dbUser() string {
if len(user) == 0 {
user = os.Getenv("PG_LLSR_TEST_USER")
if len(user) == 0 {
user = "postgres"
}
}
return user
}
func dbName() string {
if len(dbname) == 0 {
dbname = os.Getenv("PG_LLSR_TEST_DB")
if len(dbname) == 0 {
dbname = "postgres"
}
}
return dbname
}
func TestStreamOpeningAndClosing(t *testing.T) {
withTestConnection(t, func(t *testing.T, db *sql.DB) {
config := NewDatabaseConfig(dbName())
config.User = dbUser()
stream := NewStream(config, "llsr_test_slot", 0)
err := stream.Start()
if err != nil {
t.Fatal(err)
}
time.Sleep(1e9)
stream.Close()
err = <-stream.Finished()
if err != nil {
t.Fatal(err)
}
})
}
func withTestStream(t *testing.T, cb testStreamCallback) {
withTestConnection(t, func(t *testing.T, db *sql.DB) {
config := NewDatabaseConfig(dbName())
config.User = dbUser()
stream := NewStream(config, "llsr_test_slot", 0)
err := stream.Start()
if err != nil {
t.Fatal(err)
}
defer func() {
stream.Close()
err = <-stream.Finished()
if err != nil {
t.Fatal(err)
}
}()
time.Sleep(1000000000)
cb(t, db, stream)
})
}
func TestStreamMessages(t *testing.T) {
withTestStream(t, func(t *testing.T, db *sql.DB, stream *Stream) {
_, err := db.Exec("INSERT INTO llsr_test_table(id, txt) VALUES (1, 'value_1') ")
if err != nil {
t.Fatal(err)
}
_, err = db.Exec("UPDATE llsr_test_table SET txt = 'value_2' WHERE id = 1")
if err != nil {
t.Fatal(err)
}
<-stream.Data()
msg2 := <-stream.Data()
if msg2.GetTable() != "llsr_test_table" {
t.Fatal("Expected change in llsr_test_table")
}
op := msg2.GetOp()
if op != decoderbufs.Op_UPDATE {
t.Fatalf("Expected UPDATE change got %s", decoderbufs.Op_name[int32(op)])
}
})
}
func TestUnexpectedIOBehaviour(t *testing.T) {
withTestConnection(t, func(t *testing.T, db *sql.DB) {
config := NewDatabaseConfig(dbName())
config.User = dbUser()
stream := NewStream(config, "llsr_test_slot", 0)
err := stream.Start()
if err != nil {
t.Fatal(err)
}
time.Sleep(1000000000)
stream.stdOut.Close()
_, err = db.Exec("INSERT INTO llsr_test_table(id, txt) VALUES (1, 'value_1') ")
if err != nil {
t.Fatal(err)
}
select {
case err = <-stream.Finished():
if err == nil {
t.Fatalf("Expected Finished() to return error")
}
case <-time.Tick(5 * time.Second):
t.Fatal("Timeout")
}
})
}