-
Notifications
You must be signed in to change notification settings - Fork 1
/
producer_test.go
69 lines (55 loc) · 1.77 KB
/
producer_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
package gq
import (
"context"
"regexp"
"strconv"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/jmoiron/sqlx"
"github.com/mattbonnell/gq/internal"
"github.com/stretchr/testify/require"
)
func TestPushMessageShouldSucceed_OneMessage(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
m := internal.Message{Payload: []byte("random payload")}
mock.
ExpectExec(
regexp.QuoteMeta(`INSERT INTO message (payload) VALUES (?)`),
).
WithArgs(m.Payload).
WillReturnResult(sqlmock.NewResult(1, 1))
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
p, err := newProducer(ctx, sqlx.NewDb(db, arbitraryDriverName), &ProducerOptions{PushPeriod: 500 * time.Nanosecond, MaxRetryPeriods: 0, Concurrency: 1})
require.NoError(t, err)
p.Push(m.Payload)
time.Sleep(time.Millisecond)
require.NoError(t, mock.ExpectationsWereMet())
}
// TODO: fix this test
func TestPushMessageShouldSucceed_ThreeMessages(t *testing.T) {
db, mock, err := sqlmock.New()
mock.MatchExpectationsInOrder(false)
require.NoError(t, err)
messages := make([][]byte, 3)
for i := range messages {
messages[i] = []byte("payload" + strconv.Itoa(i))
}
mock.
ExpectExec(
regexp.QuoteMeta(`INSERT INTO message (payload) VALUES (?), (?), (?)`),
).
WithArgs(messages[0], messages[1], messages[2]).
WillReturnResult(sqlmock.NewResult(3, 3))
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
p, err := newProducer(ctx, sqlx.NewDb(db, arbitraryDriverName), &ProducerOptions{PushPeriod: time.Millisecond, MaxRetryPeriods: 0, Concurrency: 2})
require.NoError(t, err)
for _, m := range messages {
p.Push(m)
}
time.Sleep(time.Millisecond * 5)
require.NoError(t, mock.ExpectationsWereMet())
}