forked from Romaixn/gosumer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgsql_transport_test.go
131 lines (107 loc) · 3.71 KB
/
pgsql_transport_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
package gosumer
import (
"context"
"fmt"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/assert"
)
func TestPgConnect(t *testing.T) {
database := PgDatabase{
Host: "localhost",
Port: 5432,
User: "postgres",
Password: "postgres",
Database: "postgres",
TableName: "table_name",
}
err := database.connect()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func setupDatabase(t *testing.T) (*pgxpool.Pool, PgDatabase) {
database := PgDatabase{
Host: "localhost",
Port: 5432,
User: "postgres",
Password: "postgres",
Database: "postgres",
TableName: "table_name",
}
pool, err := pgxpool.New(context.Background(), fmt.Sprintf("postgres://%s:%s@%s:%d/%s", database.User, database.Password, database.Host, database.Port, database.Database))
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
pool.Exec(context.Background(), fmt.Sprintf("DROP TABLE IF EXISTS %s", database.TableName))
pool.Exec(context.Background(), fmt.Sprintf("CREATE TABLE %s (id BIGSERIAL NOT NULL, body TEXT NOT NULL, headers TEXT NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, available_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, delivered_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))", database.TableName))
_, err = pool.Exec(context.Background(), fmt.Sprintf("INSERT INTO %s (body, headers, queue_name, created_at, available_at, delivered_at) VALUES ('{\"id\": 1}', '{}', 'go', NOW(), NOW(), NULL)", database.TableName))
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
err = database.connect()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
return pool, database
}
func TestPgDelete(t *testing.T) {
pool, database := setupDatabase(t)
defer pool.Close()
err := database.delete(1)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
func TestPgProcessMessage(t *testing.T) {
pool, database := setupDatabase(t)
defer pool.Close()
err := database.processMessage(processMessage, Message{})
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
assert.True(t, processMessageCalled)
processMessageCalled = false
}
func TestPgListen(t *testing.T) {
pool, database := setupDatabase(t)
defer pool.Close()
go func() {
err := database.listen(processMessage, Message{}, 5)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}()
_, err := pool.Exec(context.Background(), fmt.Sprintf("INSERT INTO %s (body, headers, queue_name, created_at, available_at, delivered_at) VALUES ('{\"id\": 2}', '{}', 'go', NOW(), NOW(), NULL)", database.TableName))
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
row := pool.QueryRow(context.Background(), fmt.Sprintf("SELECT delivered_at FROM %s WHERE id = 2", database.TableName))
var deliveredAt time.Time
err = row.Scan(&deliveredAt)
if err == nil {
t.Errorf("Message was not processed: %v", err)
}
// TODO: maybe found a better way to wait for the message to be processed
time.Sleep(6 * time.Second)
assert.True(t, processMessageCalled)
processMessageCalled = false
}
func TestPgListenEvery(t *testing.T) {
pool, database := setupDatabase(t)
defer pool.Close()
go func() {
database.listenEvery(1, processMessage, Message{})
}()
row := pool.QueryRow(context.Background(), fmt.Sprintf("SELECT delivered_at FROM %s WHERE id = 1", database.TableName))
var deliveredAt time.Time
err := row.Scan(&deliveredAt)
if err == nil {
t.Errorf("message was not processed: %v", err)
}
// TODO: maybe found a better way to wait for the message to be processed
time.Sleep(2 * time.Second)
assert.True(t, processMessageCalled)
processMessageCalled = false
}