podman
63 строки · 1.4 Кб
1//go:build !windows
2
3package notifyproxy4
5import (6"testing"7"time"8
9"github.com/coreos/go-systemd/v22/daemon"10"github.com/stretchr/testify/require"11)
12
13// Helper function to send the specified message over the socket of the proxy.
14func sendMessage(t *testing.T, proxy *NotifyProxy, message string) {15err := SendMessage(proxy.SocketPath(), message)16require.NoError(t, err)17}
18
19func TestNotifyProxy(t *testing.T) {20proxy, err := New("")21require.NoError(t, err)22require.FileExists(t, proxy.SocketPath())23require.NoError(t, proxy.Close())24require.NoFileExists(t, proxy.SocketPath())25}
26
27func TestWaitAndClose(t *testing.T) {28proxy, err := New("")29require.NoError(t, err)30require.FileExists(t, proxy.SocketPath())31
32ch := make(chan error)33defer func() {34err := proxy.Close()35require.NoError(t, err, "proxy should close successfully")36}()37go func() {38ch <- proxy.Wait()39}()40
41sendMessage(t, proxy, "foo\n")42time.Sleep(250 * time.Millisecond)43select {44case err := <-ch:45t.Fatalf("Should still be waiting but received %v", err)46default:47}48
49sendMessage(t, proxy, daemon.SdNotifyReady+"\nsomething else\n")50done := func() bool {51for i := 0; i < 10; i++ {52select {53case err := <-ch:54require.NoError(t, err, "Waiting should succeed")55return true56default:57time.Sleep(time.Duration(i*250) * time.Millisecond)58}59}60return false61}()62require.True(t, done, "READY MESSAGE SHOULD HAVE ARRIVED")63}
64