summaryrefslogtreecommitdiff
path: root/pkg/systemd/notifyproxy/notifyproxy_test.go
blob: 066046cb8b3becdb1ab2cb0d4b66aa79644dbfba (plain)
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
package notifyproxy

import (
	"testing"
	"time"

	"github.com/coreos/go-systemd/v22/daemon"
	"github.com/stretchr/testify/require"
)

// Helper function to send the specified message over the socket of the proxy.
func sendMessage(t *testing.T, proxy *NotifyProxy, message string) {
	err := SendMessage(proxy.SocketPath(), message)
	require.NoError(t, err)
}

func TestNotifyProxy(t *testing.T) {
	proxy, err := New("")
	require.NoError(t, err)
	require.FileExists(t, proxy.SocketPath())
	require.NoError(t, proxy.close())
	require.NoFileExists(t, proxy.SocketPath())
}

func TestWaitAndClose(t *testing.T) {
	proxy, err := New("")
	require.NoError(t, err)
	require.FileExists(t, proxy.SocketPath())

	ch := make(chan error)

	go func() {
		ch <- proxy.WaitAndClose()
	}()

	sendMessage(t, proxy, "foo\n")
	time.Sleep(250 * time.Millisecond)
	select {
	case err := <-ch:
		t.Fatalf("Should still be waiting but received %v", err)
	default:
	}

	sendMessage(t, proxy, daemon.SdNotifyReady+"\nsomething else\n")
	done := func() bool {
		for i := 0; i < 10; i++ {
			select {
			case err := <-ch:
				require.NoError(t, err, "Waiting should succeed")
				return true
			default:
				time.Sleep(time.Duration(i*250) * time.Millisecond)
			}
		}
		return false
	}()
	require.True(t, done, "READY MESSAGE SHOULD HAVE ARRIVED")
}