-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsqlx_test.go
More file actions
87 lines (80 loc) · 2.15 KB
/
sqlx_test.go
File metadata and controls
87 lines (80 loc) · 2.15 KB
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
package outbox
import (
"context"
"fmt"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/nilorg/eventbus"
)
func newTestSqlxEngine(t *testing.T) (engine Engine) {
var (
err error
db *sqlx.DB
)
db, err = sqlx.Connect("mysql", "root:test123@tcp(127.0.0.1:3306)/outbox?charset=utf8&parseTime=True&loc=Local")
if err != nil {
t.Fatalf("sqlx connect error: %v", err)
}
// 注意: AutoMigrate 会在 New 函数内部自动调用,无需手动创建表
bus := newTestEventBus(t)
opts := &EngineOptions{
FailedRetryInterval: time.Minute,
FailedRetryCount: 50,
DataCleanInterval: time.Hour,
SucceedMessageExpiredAfter: 24 * time.Hour,
SnowflakeNode: 1,
Logger: &eventbus.StdLogger{},
}
engine, err = New(EngineTypeSqlx, db, bus, opts)
if err != nil {
t.Fatalf("new sqlx engine error: %s", err)
}
return
}
func TestSqlx(t *testing.T) {
engine := newTestSqlxEngine(t)
ctx := context.Background()
var err error
topic := "order.create.success.sync"
ctxGroup1 := eventbus.NewGroupIDContext(ctx, "nilorg.events.sync.group1")
go func() {
err = engine.Subscribe(ctxGroup1, topic, func(ctx context.Context, msg *Message) error {
fmt.Printf("group1 %s: %+v is timeout: %v\n", topic, msg, msg.IsTimeout(3*time.Minute))
return nil
})
if err != nil {
t.Error(err)
return
}
}()
go func() {
err = engine.Subscribe(ctxGroup1, topic, func(ctx context.Context, msg *Message) error {
fmt.Printf("group1(copy) %s: %+v is timeout: %v\n", topic, msg, msg.IsTimeout(3*time.Minute))
return nil
})
if err != nil {
t.Error(err)
return
}
}()
ctxGroup2 := eventbus.NewGroupIDContext(ctx, "nilorg.events.sync.group2")
go func() {
err = engine.Subscribe(ctxGroup2, topic, func(ctx context.Context, msg *Message) error {
fmt.Printf("group2 %s: %+v is timeout: %v\n", topic, msg, msg.IsTimeout(3*time.Minute))
return nil
})
if err != nil {
t.Error(err)
return
}
}()
time.Sleep(1 * time.Second)
err = engine.Publish(ctx, topic, "publish 测试")
if err != nil {
t.Error(err)
return
}
time.Sleep(2 * time.Second)
}