fork/gdu

git clone https://git.julia.blue/fork/gdu

Daniel Mildetest: cover internal packages (#535)fb46b4f

master
1.1 KiB60 linesraw
1package common
2
3import (
4	"testing"
5	"time"
6
7	"github.com/stretchr/testify/assert"
8)
9
10func TestSignalGroupWait(t *testing.T) {
11	sg := make(SignalGroup)
12	done := make(chan struct{})
13
14	go func() {
15		sg.Wait()
16		done <- struct{}{}
17	}()
18
19	// Give the goroutine time to block on Wait()
20	time.Sleep(10 * time.Millisecond)
21
22	// Broadcast should unblock the Wait
23	sg.Broadcast()
24
25	// Verify goroutine exited
26	select {
27	case <-done:
28		// Success
29	case <-time.After(100 * time.Millisecond):
30		t.Fatal("Wait() did not unblock after Broadcast()")
31	}
32}
33
34func TestSignalGroupBroadcast(t *testing.T) {
35	sg := make(SignalGroup)
36
37	// Broadcast should not panic on a fresh channel
38	assert.NotPanics(t, func() {
39		sg.Broadcast()
40	})
41}
42
43func TestSignalGroupWaitAfterBroadcast(t *testing.T) {
44	sg := make(SignalGroup)
45	sg.Broadcast()
46
47	// Wait on already-closed channel should return immediately
48	done := make(chan struct{})
49	go func() {
50		sg.Wait()
51		done <- struct{}{}
52	}()
53
54	select {
55	case <-done:
56		// Success
57	case <-time.After(100 * time.Millisecond):
58		t.Fatal("Wait() did not return after already-broadcast channel")
59	}
60}