-
Notifications
You must be signed in to change notification settings - Fork 318
/
module3_test.go
120 lines (107 loc) · 2.77 KB
/
module3_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
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
package alog
import (
"bytes"
"strings"
"testing"
"time"
)
// 01
func TestNewInitializesShutdownChannelsModule3(t *testing.T) {
alog := New(nil)
if alog.shutdownCh == nil {
t.Error("shutdownCh field not initialized")
}
if alog.shutdownCompleteCh == nil {
t.Error("shutdownCompleteCh field not initialized")
}
}
// 02
func TestShutdownMethodModule3(t *testing.T) {
alog := New(nil)
alog.shutdownCompleteCh = make(chan struct{}, 1)
alog.shutdown()
time.Sleep(100 * time.Millisecond)
select {
case _, ok := <-alog.msgCh:
if ok {
t.Error("msgCh not closed by shutdown() method")
}
default:
t.Error("msgCh not closed by shutdown() method")
}
select {
case <-alog.shutdownCompleteCh:
default:
t.Error("shutdown() doesn't send message to shutdownCompleteCh")
}
}
// 03
func TestStartMethodCallsShutdownModule3(t *testing.T) {
b := bytes.NewBuffer([]byte{})
alog := New(b)
alog.shutdownCh = make(chan struct{}, 1)
alog.shutdownCompleteCh = make(chan struct{}, 1)
go alog.Start()
alog.shutdownCh <- struct{}{}
time.Sleep(100 * time.Millisecond)
select {
case _, ok := <-alog.msgCh:
if ok {
t.Error("Passing message to shutdownCh doesn't call shutdown()")
}
default:
t.Error("Passing message to shutdownCh doesn't call shutdown()")
}
select {
case <-alog.shutdownCompleteCh:
default:
t.Error("Passing message to shutdownCh doesn't call shutdown()")
}
if b.Len() != 0 {
t.Error("Passing message to shutdownCh doesn't break out of the Start method's for loop. " +
"Note that 'break' statements can be used for select and for loops so a label might be " +
"required to break out the loop.")
}
}
// 04
func TestStopMethodModule3(t *testing.T) {
alog := New(nil)
alog.shutdownCh = make(chan struct{}, 1)
alog.shutdownCompleteCh = make(chan struct{}, 1)
alog.shutdownCompleteCh <- struct{}{}
alog.Stop()
select {
case <-alog.shutdownCh:
default:
t.Error("Stop() method doesn't send signal to shutdownCh channel")
}
select {
case <-alog.shutdownCompleteCh:
t.Error("Stop() method doesn't wait for signal from shutdownCompleteCh channel")
default:
}
}
// 05
func TestWriteAllBeforeShutdownModule3(t *testing.T) {
b := bytes.NewBuffer([]byte{})
alog := New(sleepingWriter{b})
alog.msgCh = make(chan string, 2)
go alog.Start()
alog.msgCh <- "first"
alog.msgCh <- "second"
time.Sleep(10 * time.Millisecond)
doneCh := make(chan struct{})
go func() {
alog.Stop()
written := b.String()
if !strings.Contains(written, "first") || !strings.Contains(written, "second") {
t.Error("Not all messages written before logger shutdown")
}
doneCh <- struct{}{}
}()
select {
case <-time.Tick(1 * time.Second):
t.Error("Test timed out, please check that the Done method on the wait group is being called in the write method")
case <-doneCh:
}
}