-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_test.go
47 lines (45 loc) · 1.12 KB
/
async_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
package wglog
import (
"github.com/stretchr/testify/require"
"golang.zx2c4.com/wireguard/device"
"runtime"
"sync"
"testing"
)
func TestAsync(t *testing.T) {
t.Run("normal", func(t *testing.T) {
var v, va, e, ea bool
var w sync.WaitGroup
w.Add(2)
l := Async(&device.Logger{
Verbosef: func(string, ...any) {
defer w.Done()
v = true
// Check if func was called with `go`
// https://stackoverflow.com/a/56702614
va = runtime.Callers(3, make([]uintptr, 1)) == 0
},
Errorf: func(string, ...any) {
defer w.Done()
e = true
// Check if func was called with `go`
// https://stackoverflow.com/a/56702614
ea = runtime.Callers(3, make([]uintptr, 1)) == 0
},
})
l.Verbosef("")
l.Errorf("")
w.Wait()
require.True(t, v, "failed to call verbose")
require.True(t, va, "failed to call verbose in a goroutine")
require.True(t, e, "failed to call error")
require.True(t, ea, "failed to call error in a goroutine")
})
t.Run("noop", func(t *testing.T) {
l := Async(new(device.Logger))
require.NotNil(t, l.Verbosef)
require.NotNil(t, l.Errorf)
l.Verbosef("")
l.Errorf("")
})
}