-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathThread_test.go
97 lines (76 loc) · 2.06 KB
/
Thread_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
package pthread
import (
"github.com/alecthomas/gozmq"
"testing"
"time"
)
func Test_ZMQ(t *testing.T) {
// initialize some ZMQ crap
failed := false
context, _ := gozmq.NewContext()
socket, _ := context.NewSocket(gozmq.REP)
socket.Bind("tcp://127.0.0.1:9898")
// create the thread
thread := Create(func() {
// this call should block forever, and thread.Kill() should stop it
socket.Recv(0)
// this should never happen
failed = true
})
// wait around for a bit
time.Sleep(time.Millisecond * 100)
// make sure the thread started
if !thread.Running() {
t.Error("The thread was not running after starting it")
}
// stop the thread
thread.Kill()
// wait around a bit more
time.Sleep(time.Millisecond * 100)
// make sure the thread terminated
if thread.Running() {
t.Error("The thread was still running after the kill signal")
}
// finally, make sure that the thread didn't go to a point in execution that
// it was not supposed to reach
if failed {
// this might be more revealing of a bug in zmq or pthreads, but what do
// i know
t.Error("The recv call did not block or the thread did not exit properly.")
}
}
func Test_ManyThreads(t *testing.T) {
literal := func() {
for {
Sleep(1); // sleep for 1s, forever
}
}
// start up the threads
thread1 := Create(literal);
thread2 := Create(literal);
// give them some time to spin up
time.Sleep(time.Millisecond * 100);
// ensure both started
if !thread1.Running() || !thread2.Running() {
t.Error("One or both of the threads failed to start properly");
}
// kill one of the threads
thread1.Kill();
// give it some time to clean up
time.Sleep(time.Millisecond * 100);
// ensure one has exited and the other has running
if thread1.Running() {
t.Error("Thread 1 has failed to stop");
}
if !thread2.Running() {
t.Error("Thread 2 stopped when it shouldn't have");
}
// stop the second thread
thread2.Kill();
// wait for it to spin down
time.Sleep(time.Millisecond * 100);
// ensure that it has spun down
if thread2.Running() {
t.Error("Thread 2 has failed to stop");
}
}