-
Notifications
You must be signed in to change notification settings - Fork 5
/
transport_test.go
63 lines (46 loc) · 1.21 KB
/
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
package gosumer
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestFormatMessage(t *testing.T) {
var msg any
jsonData := `{"id": 1, "name": "John Doe"}`
_, err := formatMessage(jsonData, &msg)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
invalidJsonData := `{"id": 1, "name": "John Doe"`
_, err = formatMessage(invalidJsonData, &msg)
if err == nil {
t.Errorf("Expected an error for invalid JSON data, but got none")
}
}
type TestTransport struct{}
func (transport TestTransport) connect() error {
return nil
}
func (transport TestTransport) listen(fn process, message any, sec int) error {
go fn(message, make(chan error))
return nil
}
var processMessageCalled = false
func processMessage(message any, err chan error) {
processMessageCalled = true
err <- nil
}
type Message struct {
ID int `json:"id"`
}
func TestListen(t *testing.T) {
transport := TestTransport{}
err := Listen(transport, processMessage, Message{}, 5)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
// TODO: maybe found a better way to wait for the message to be processed
time.Sleep(1 * time.Second)
assert.True(t, processMessageCalled)
processMessageCalled = false
}