-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaitingAppStart.go
76 lines (62 loc) · 1.79 KB
/
WaitingAppStart.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
package main
import (
"context"
"fmt"
"github.com/kneu-messenger-pigeon/events"
"github.com/segmentio/kafka-go"
"os"
"strings"
"time"
)
func WaitKafkaEvent(topicName string, eventName string) {
var err error
var m kafka.Message
reader := kafka.NewReader(
kafka.ReaderConfig{
Brokers: []string{config.kafkaHost},
Topic: topicName,
MinBytes: 10,
MaxBytes: 10e3,
MaxWait: time.Second,
MaxAttempts: config.kafkaAttempts,
Dialer: &kafka.Dialer{
Timeout: config.kafkaTimeout,
DualStack: kafka.DefaultDialer.DualStack,
},
},
)
defer reader.Close()
ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Minute)
eventReceivedCount := 0
fmt.Println("Waiting for " + eventName + " event...")
for ctx.Err() == nil {
m, err = reader.FetchMessage(ctx)
if err != nil && err != context.DeadlineExceeded {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
} else if strings.HasPrefix(string(m.Key), eventName) {
eventReceivedCount++
// wait for repeat event during 5 seconds
cancelFunc()
ctx, cancelFunc = context.WithTimeout(context.Background(), 5*time.Second)
}
}
cancelFunc()
if eventReceivedCount == 0 {
fmt.Println(eventName + " is not happen")
} else {
fmt.Printf("Receive %s - count %d \n", eventName, eventReceivedCount)
}
}
func WaitSecondaryDbScoreProcessedEvent() {
WaitKafkaEvent(events.MetaEventsTopic, events.SecondaryDbScoreProcessedEventName)
}
func WaitScoreChangedEvent() {
WaitKafkaEvent(events.ScoresChangesFeedTopic, events.ScoreChangedEventName)
}
func WaitTelegramAppStarted() {
fmt.Print("waiting for call telegramAPI /getMe ")
waitUntilCalled(mocks.TelegramMockServer.scopedGetMe, time.Minute)
if mocks.TelegramMockServer.scopedGetMe.IsPending() {
fmt.Println("getMeMock is still pending")
}
}