From 81ff899320ed9cf9ac95560cf31ecfb12fab9fec Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 15 Feb 2022 11:48:22 -0800 Subject: [PATCH 1/2] TestObjectSignalHandling: fix/improve bus.Emit error was not checked, and since otherPath was invalid (had a hyphen), the signal was never emitted. Fix both issues. Signed-off-by: Kir Kolyshkin --- object_test.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/object_test.go b/object_test.go index 1e9ba1f..a7c31cd 100644 --- a/object_test.go +++ b/object_test.go @@ -85,7 +85,7 @@ func TestObjectSignalHandling(t *testing.T) { name := bus.Names()[0] path := ObjectPath("/org/godbus/DBus/TestSignals") - otherPath := ObjectPath("/org/other-godbus/DBus/TestSignals") + otherPath := ObjectPath("/org/other/godbus/DBus/TestSignals") iface := "org.godbus.DBus.TestSignals" otherIface := "org.godbus.DBus.OtherTestSignals" err = bus.Export(nopServer{}, path, iface) @@ -112,18 +112,25 @@ func TestObjectSignalHandling(t *testing.T) { } }() + emit := func(path ObjectPath, name string, values ...interface{}) { + t.Helper() + if err := bus.Emit(path, name, values...); err != nil { + t.Error("Emit:", err) + } + } + // desired signals - bus.Emit(path, iface+".Heartbeat", uint32(1)) - bus.Emit(path, iface+".Heartbeat", uint32(2)) + emit(path, iface+".Heartbeat", uint32(1)) + emit(path, iface+".Heartbeat", uint32(2)) // undesired signals - bus.Emit(otherPath, iface+".Heartbeat", uint32(3)) - bus.Emit(otherPath, otherIface+".Heartbeat", uint32(4)) - bus.Emit(path, iface+".Updated", false) + emit(otherPath, iface+".Heartbeat", uint32(3)) + emit(otherPath, otherIface+".Heartbeat", uint32(4)) + emit(path, iface+".Updated", false) // sentinel - bus.Emit(path, iface+".Heartbeat", uint32(5)) + emit(path, iface+".Heartbeat", uint32(5)) time.Sleep(100 * time.Millisecond) - bus.Emit(path, iface+".Heartbeat", uint32(6)) + emit(path, iface+".Heartbeat", uint32(6)) }() checkSignal := func(sig *Signal, value uint32) { From 2c3cf657d630429b62f481c930c754a818d98327 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 15 Feb 2022 11:58:31 -0800 Subject: [PATCH 2/2] TestObjectSignalHandling: minor refactor - embed fetchSignal into checkSignal; - remove t and timeout arguments; - add t.Helper so that errors show correct line number; Signed-off-by: Kir Kolyshkin --- object_test.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/object_test.go b/object_test.go index a7c31cd..64be90e 100644 --- a/object_test.go +++ b/object_test.go @@ -66,16 +66,6 @@ func (_ nopServer) Nop() *Error { return nil } -func fetchSignal(t *testing.T, ch chan *Signal, timeout time.Duration) *Signal { - select { - case sig := <-ch: - return sig - case <-time.After(timeout): - t.Fatalf("Failed to fetch signal in specified timeout %s", timeout) - } - return nil -} - func TestObjectSignalHandling(t *testing.T) { bus, err := ConnectSessionBus() if err != nil { @@ -133,7 +123,19 @@ func TestObjectSignalHandling(t *testing.T) { emit(path, iface+".Heartbeat", uint32(6)) }() - checkSignal := func(sig *Signal, value uint32) { + checkSignal := func(ch chan *Signal, value uint32) { + t.Helper() + + const timeout = 50 * time.Millisecond + var sig *Signal + + select { + case sig = <-ch: + // do nothing + case <-time.After(timeout): + t.Fatalf("Failed to fetch signal in specified timeout %s", timeout) + } + if sig.Path != path { t.Errorf("signal.Path mismatch: %s != %s", path, sig.Path) } @@ -153,9 +155,9 @@ func TestObjectSignalHandling(t *testing.T) { } } - checkSignal(fetchSignal(t, ch, 50*time.Millisecond), 1) - checkSignal(fetchSignal(t, ch, 50*time.Millisecond), 2) - checkSignal(fetchSignal(t, ch, 50*time.Millisecond), 5) + checkSignal(ch, 1) + checkSignal(ch, 2) + checkSignal(ch, 5) obj.RemoveMatchSignal(iface, "Heartbeat", WithMatchObjectPath(obj.Path())) select {