File size: 3,559 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package harness

import (
	"sync"
	"testing"
	"time"

	"github.com/target/goalert/devtools/mocktwilio"
)

type twilioAssertionAPIContext struct {
	t *testing.T
	*twilioAssertionAPI
}

func (w *twilioAssertionAPIContext) Device(number string) PhoneDevice {
	return w.twilioAssertionAPI.Device(w.t, number)
}
func (w *twilioAssertionAPIContext) WaitAndAssert() { w.twilioAssertionAPI.WaitAndAssert(w.t) }

type twilioAssertionAPI struct {
	*mocktwilio.Server

	triggerFn   func()
	sendSMSDest string
	messages    []*mocktwilio.SMS
	calls       []*mocktwilio.VoiceCall

	activeCalls []*mocktwilio.VoiceCall

	ignoredSMS   anyMessage
	ignoredVoice anyMessage

	formatNumber func(string) string

	mx sync.Mutex

	abortCh chan struct{}
}

func newTwilioAssertionAPI(triggerFn func(), formatNumber func(string) string, srv *mocktwilio.Server, sendSMSDest string) *twilioAssertionAPI {
	return &twilioAssertionAPI{
		triggerFn:    triggerFn,
		Server:       srv,
		sendSMSDest:  sendSMSDest,
		formatNumber: formatNumber,
		abortCh:      make(chan struct{}),
	}
}
func (tw *twilioAssertionAPI) Close() { close(tw.abortCh); tw.Server.Close() }

func (tw *twilioAssertionAPI) WithT(t *testing.T) PhoneAssertions {
	return &twilioAssertionAPIContext{t: t, twilioAssertionAPI: tw}
}

func (tw *twilioAssertionAPI) Device(t *testing.T, number string) PhoneDevice {
	return &twilioAssertionDevice{
		twilioAssertionAPIContext: &twilioAssertionAPIContext{t: t, twilioAssertionAPI: tw},

		number: number,
	}
}

func (tw *twilioAssertionAPI) triggerTimeout() (<-chan string, func()) {
	cancelCh := make(chan struct{})

	errMsgCh := make(chan string, 1)
	t := time.NewTimer(10 * time.Second)
	go func() {
		defer t.Stop()

		// 3 engine cycles, or timeout/cancel (whichever is sooner)
		for i := 0; i < 3; i++ {
			select {
			case <-tw.abortCh:
				errMsgCh <- "test exiting"
				return
			case <-t.C:
				errMsgCh <- "10 seconds"
				return
			default:
				tw.triggerFn()
			}
		}
		select {
		case <-t.C:
			errMsgCh <- "10 seconds"
		case <-tw.abortCh:
			errMsgCh <- "test exiting"
			return
		}
	}()

	return errMsgCh, func() { close(cancelCh) }
}

func (tw *twilioAssertionAPI) WaitAndAssert(t *testing.T) {
	t.Helper()
	if t.Failed() {
		// don't wait if test has already failed
		return
	}
	tw.mx.Lock()
	defer tw.mx.Unlock()
	t.Log("WaitAndAssert: waiting for unexpected messages")

	for _, sms := range tw.messages {
		if tw.ignoredSMS.match(sms) {
			continue
		}
		t.Fatalf("found unexpected SMS to %s: %s", tw.formatNumber(sms.To()), sms.Body())
	}
	for _, call := range tw.calls {
		if tw.ignoredVoice.match(call) {
			continue
		}
		t.Fatalf("found unexpected voice call to %s: %s", tw.formatNumber(call.To()), call.Body())
	}

	timeout, cancel := tw.triggerTimeout()
	defer cancel()
waitLoop:
	for {
		select {
		case sms := <-tw.SMS():
			if tw.ignoredSMS.match(sms) {
				sms.Accept()
				continue
			}
			t.Fatalf("got unexpected SMS to %s: %s", tw.formatNumber(sms.To()), sms.Body())
		case call := <-tw.VoiceCalls():
			if tw.ignoredVoice.match(call) {
				call.Accept()
				call.Hangup()
				continue
			}
			t.Fatalf("got unexpected voice call to %s: %s", tw.formatNumber(call.To()), call.Body())
		case <-timeout:
			break waitLoop
		}
	}

	tw.ignoredSMS = nil
	tw.ignoredVoice = nil
	tw.calls = nil
	tw.messages = nil
	for _, call := range tw.activeCalls {
		call.Hangup()
	}
	tw.activeCalls = nil
}

// Twilio will return PhoneAssertions for the given testing context.
func (h *Harness) Twilio(t *testing.T) PhoneAssertions { return h.tw.WithT(t) }