-
Notifications
You must be signed in to change notification settings - Fork 12
/
thread_setup_test.go
40 lines (35 loc) · 1.12 KB
/
thread_setup_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
package openssl
import (
"runtime"
"testing"
"time"
)
func init() {
// The runtime parks the "main" thread of the process on Linux rather of
// terminating it. Lock the main thread to the initial goroutine to ensure
// that the test goroutines will always be scheduled onto non-main threads
// that can be consistently made to terminate on demand.
runtime.LockOSThread()
}
func TestThreadCleanup(t *testing.T) {
if vMajor > 1 || vMinor > 0 {
t.Skip("explicit thread cleanup is only needed for OpenSSL 1.0.x")
}
before := opensslThreadsCleanedUp()
done := make(chan struct{})
go func() {
defer close(done)
// The thread this goroutine is running on will be terminated by the
// runtime when the goroutine exits.
runtime.LockOSThread()
// Checking for errors has the side effect of initializing
// the thread-local OpenSSL error queue.
_ = newOpenSSLError("")
}()
<-done
time.Sleep(100 * time.Millisecond) // Give some time for the thread to terminate.
after := opensslThreadsCleanedUp()
if n := after - before; n != 1 {
t.Errorf("expected thread cleanup to have run once, but it ran %d times", n)
}
}