-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync_test.go
51 lines (38 loc) · 967 Bytes
/
sync_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
// Copyright (c) 2020 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package longroutine_test
import (
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/dispatchframework/longroutine"
)
func TestSyncStarter_Start(t *testing.T) {
starter := longroutine.NewSingleStarter()
started := &sync.WaitGroup{}
started.Add(1)
run := &sync.WaitGroup{}
run.Add(1)
c := make(chan struct{}, 10)
for range [10]struct{}{} {
starter.StartSingle("one-long", func() {
c <- struct{}{}
started.Done() // will rightfully panic if more than one of these are called concurrently
run.Wait() // simulating a long running routine
})
}
started.Wait()
require.Equal(t, 1, len(c))
<-c
require.Equal(t, 0, len(c))
for range [10]struct{}{} {
started.Wait()
started.Add(1)
starter.StartSingle("many-short", func() {
c <- struct{}{}
started.Done()
})
}
started.Wait()
require.Equal(t, 10, len(c))
}