/
producer_api_test.go
84 lines (73 loc) · 1.56 KB
/
producer_api_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
package haystack
import (
"log"
"testing"
"time"
)
const (
tempStreamName string = "PRODUCER_TEST"
)
// initialise connection to Kinesis instance
func TestKinesisInit(t *testing.T) {
if _, ok := InitKinesis(); !ok {
log.Println("Failed to init Kinesis connection.")
t.Fail()
}
}
// check if stream exists
func TestCheckStream(t *testing.T) {
if k, ok := InitKinesis(); ok {
if test := CheckStream(k, "RANDOM_STREAM_NAME"); test {
t.Fail()
}
}
}
// open or create a kinesis stream
func TestStreamInit(t *testing.T) {
if k, ok := InitKinesis(); ok {
// create a new stream
if ok := CreateStream(k, tempStreamName, 1); !ok {
t.Fail()
}
// creating for the second time should fail
if ok := CreateStream(k, tempStreamName, 1); ok {
t.Fail()
}
}
}
// put message into stream
func TestSendMessage(t *testing.T) {
ch := make(chan bool)
tempMessage := &Message{
"1234", // uid
"test.com/testpage.html", // ref site
time.Now().String(), // timestamp
"Pageview", // event type
"{'a': 1}"}
if k, ok := InitKinesis(); ok {
go SendMessage(k, tempStreamName, tempMessage, ch)
}
// Hang around for a response
select {
case resp := <-ch:
if !resp {
log.Println("Message failed to send")
t.Fail()
}
return
case <-time.After(10 * time.Second):
log.Println("Message send timeout")
t.Fail()
return
}
}
// delete stream
func TestDeleteStream(t *testing.T) {
if k, ok := InitKinesis(); ok {
if ok := DeleteStream(k, tempStreamName); ok {
return
}
}
log.Println("Failed to delete stream")
t.Fail()
}