-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka_client_inegration_test.go
78 lines (70 loc) · 2.16 KB
/
kafka_client_inegration_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
// +build integration
package melonade_client_go
import (
"fmt"
"log"
"sync"
"testing"
"time"
)
func TestWorkerClient(t *testing.T) {
t.Run("Create worker client", func(t *testing.T) {
const batchNumber = 120
transactionPrefix := time.Now().Format("2006-01-02T-15-04-05")
cli, err := NewKafkaClient("localhost:29092", "docker-compose", "2.3.1")
if err != nil {
t.Errorf(`should create client without error: %v`, err)
}
taskNames := []string{"t1", "t2", "t3"}
var wg sync.WaitGroup
for _, tn := range taskNames {
err := cli.NewWorker(
tn,
func(t *Task) *TaskResult {
tr := NewTaskResult(t)
tr.Status = TaskStatusCompleted
tr.Output = fmt.Sprintf(`Hello from %v`, t.TaskName)
log.Printf(`Procressing %s (%s)`, t.TaskName, t.TaskID)
return tr
},
func(t *Task) *TaskResult {
tr := NewTaskResult(t)
tr.Status = TaskStatusCompleted
tr.Output = fmt.Sprintf(`Hello from %v`, t.TaskName)
log.Printf(`Compensating %s (%s)`, t.TaskName, t.TaskID)
return tr
},
)
if err != nil {
t.Errorf(`should create worker without error: %v`, err)
}
}
watcher, err := cli.NewEventWatcher("local-test")
totalTransactionEvent := batchNumber * 2
totalWorkflowEvent := batchNumber * 2
wg.Add(totalTransactionEvent + totalWorkflowEvent)
go func() {
for {
e := <-watcher
switch e.(type) {
case *EventTransaction:
td := e.(*EventTransaction)
log.Printf("Transaction %s was %s\n", td.TransactionID, td.Details.Status)
wg.Done()
case *EventWorkflow:
wd := e.(*EventWorkflow)
log.Printf("Workflow %s was %s\n", wd.Details.WorkflowID, wd.Details.Status)
wg.Done()
}
}
}()
time.Sleep(10 * time.Second) // Wait for kafka consumer group rebalanced
for i := 0; i < batchNumber; i++ {
transactionID := fmt.Sprintf(`test-%s-%d`, transactionPrefix, i)
go cli.StartTransaction(transactionID, "simple", "1", nil, []string{"test"})
log.Printf("Started transaction %s\n", transactionID)
}
wg.Wait() // Wait for all worker to finish (if everything work correctly)
time.Sleep(3 * time.Second) // Wait for producer sent message
})
}