File: tomb_test.go

package info (click to toggle)
golang-gopkg-tomb.v2 0.0~git20161208.0.d5d1b58-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84 kB
  • sloc: makefile: 3
file content (183 lines) | stat: -rw-r--r-- 3,933 bytes parent folder | download | duplicates (6)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package tomb_test

import (
	"errors"
	"gopkg.in/tomb.v2"
	"reflect"
	"testing"
)

func nothing() error { return nil }

func TestNewTomb(t *testing.T) {
	tb := &tomb.Tomb{}
	checkState(t, tb, false, false, tomb.ErrStillAlive)
}

func TestGo(t *testing.T) {
	tb := &tomb.Tomb{}
	alive := make(chan bool)
	tb.Go(func() error {
		alive <- true
		tb.Go(func() error {
			alive <- true
			<-tb.Dying()
			return nil
		})
		<-tb.Dying()
		return nil
	})
	<-alive
	<-alive
	checkState(t, tb, false, false, tomb.ErrStillAlive)
	tb.Kill(nil)
	tb.Wait()
	checkState(t, tb, true, true, nil)
}

func TestGoErr(t *testing.T) {
	first := errors.New("first error")
	second := errors.New("first error")
	tb := &tomb.Tomb{}
	alive := make(chan bool)
	tb.Go(func() error {
		alive <- true
		tb.Go(func() error {
			alive <- true
			return first
		})
		<-tb.Dying()
		return second
	})
	<-alive
	<-alive
	tb.Wait()
	checkState(t, tb, true, true, first)
}

func TestGoPanic(t *testing.T) {
	// ErrDying being used properly, after a clean death.
	tb := &tomb.Tomb{}
	tb.Go(nothing)
	tb.Wait()
	defer func() {
		err := recover()
		if err != "tomb.Go called after all goroutines terminated" {
			t.Fatalf("Wrong panic on post-death tomb.Go call: %v", err)
		}
		checkState(t, tb, true, true, nil)
	}()
	tb.Go(nothing)
}

func TestKill(t *testing.T) {
	// a nil reason flags the goroutine as dying
	tb := &tomb.Tomb{}
	tb.Kill(nil)
	checkState(t, tb, true, false, nil)

	// a non-nil reason now will override Kill
	err := errors.New("some error")
	tb.Kill(err)
	checkState(t, tb, true, false, err)

	// another non-nil reason won't replace the first one
	tb.Kill(errors.New("ignore me"))
	checkState(t, tb, true, false, err)

	tb.Go(nothing)
	tb.Wait()
	checkState(t, tb, true, true, err)
}

func TestKillf(t *testing.T) {
	tb := &tomb.Tomb{}

	err := tb.Killf("BO%s", "OM")
	if s := err.Error(); s != "BOOM" {
		t.Fatalf(`Killf("BO%s", "OM"): want "BOOM", got %q`, s)
	}
	checkState(t, tb, true, false, err)

	// another non-nil reason won't replace the first one
	tb.Killf("ignore me")
	checkState(t, tb, true, false, err)

	tb.Go(nothing)
	tb.Wait()
	checkState(t, tb, true, true, err)
}

func TestErrDying(t *testing.T) {
	// ErrDying being used properly, after a clean death.
	tb := &tomb.Tomb{}
	tb.Kill(nil)
	tb.Kill(tomb.ErrDying)
	checkState(t, tb, true, false, nil)

	// ErrDying being used properly, after an errorful death.
	err := errors.New("some error")
	tb.Kill(err)
	tb.Kill(tomb.ErrDying)
	checkState(t, tb, true, false, err)

	// ErrDying being used badly, with an alive tomb.
	tb = &tomb.Tomb{}
	defer func() {
		err := recover()
		if err != "tomb: Kill with ErrDying while still alive" {
			t.Fatalf("Wrong panic on Kill(ErrDying): %v", err)
		}
		checkState(t, tb, false, false, tomb.ErrStillAlive)
	}()
	tb.Kill(tomb.ErrDying)
}

func TestKillErrStillAlivePanic(t *testing.T) {
	tb := &tomb.Tomb{}
	defer func() {
		err := recover()
		if err != "tomb: Kill with ErrStillAlive" {
			t.Fatalf("Wrong panic on Kill(ErrStillAlive): %v", err)
		}
		checkState(t, tb, false, false, tomb.ErrStillAlive)
	}()
	tb.Kill(tomb.ErrStillAlive)
}

func checkState(t *testing.T, tb *tomb.Tomb, wantDying, wantDead bool, wantErr error) {
	select {
	case <-tb.Dying():
		if !wantDying {
			t.Error("<-Dying: should block")
		}
	default:
		if wantDying {
			t.Error("<-Dying: should not block")
		}
	}
	seemsDead := false
	select {
	case <-tb.Dead():
		if !wantDead {
			t.Error("<-Dead: should block")
		}
		seemsDead = true
	default:
		if wantDead {
			t.Error("<-Dead: should not block")
		}
	}
	if err := tb.Err(); err != wantErr {
		t.Errorf("Err: want %#v, got %#v", wantErr, err)
	}
	if wantDead && seemsDead {
		waitErr := tb.Wait()
		switch {
		case waitErr == tomb.ErrStillAlive:
			t.Errorf("Wait should not return ErrStillAlive")
		case !reflect.DeepEqual(waitErr, wantErr):
			t.Errorf("Wait: want %#v, got %#v", wantErr, waitErr)
		}
	}
}