Skip to content

Commit bcaa439

Browse files
committed
std.os: improve sigaction test coverage
1 parent 7f1f2e6 commit bcaa439

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

lib/std/os/test.zig

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,6 @@ test "shutdown socket" {
739739
os.closeSocket(sock);
740740
}
741741

742-
var signal_test_handler_called_count: u32 = 0;
743-
744742
test "sigaction" {
745743
if (native_os == .wasi or native_os == .windows)
746744
return error.SkipZigTest;
@@ -750,17 +748,19 @@ test "sigaction" {
750748
return error.SkipZigTest;
751749

752750
const S = struct {
751+
var handler_called_count: u32 = 0;
752+
753753
fn handler(sig: i32, info: *const os.siginfo_t, ctx_ptr: ?*const anyopaque) callconv(.C) void {
754754
_ = ctx_ptr;
755755
// Check that we received the correct signal.
756756
switch (native_os) {
757757
.netbsd => {
758758
if (sig == os.SIG.USR1 and sig == info.info.signo)
759-
signal_test_handler_called_count += 1;
759+
handler_called_count += 1;
760760
},
761761
else => {
762762
if (sig == os.SIG.USR1 and sig == info.signo)
763-
signal_test_handler_called_count += 1;
763+
handler_called_count += 1;
764764
},
765765
}
766766
}
@@ -774,25 +774,39 @@ test "sigaction" {
774774
.flags = os.SA.SIGINFO | os.SA.RESETHAND,
775775
};
776776
var old_sa: os.Sigaction = undefined;
777+
777778
// Install the new signal handler.
778779
try os.sigaction(os.SIG.USR1, &sa, null);
780+
779781
// Check that we can read it back correctly.
780782
try os.sigaction(os.SIG.USR1, null, &old_sa);
781783
try testing.expectEqual(actual_handler, old_sa.handler.sigaction.?);
782784
try testing.expect((old_sa.flags & os.SA.SIGINFO) != 0);
785+
783786
// Invoke the handler.
784787
try os.raise(os.SIG.USR1);
785-
try testing.expect(signal_test_handler_called_count == 1);
786-
// Check if the handler has been correctly reset to SIG_DFL
788+
try testing.expect(S.handler_called_count == 1);
789+
790+
// Check if passing RESETHAND correctly reset the handler to SIG_DFL
787791
try os.sigaction(os.SIG.USR1, null, &old_sa);
788792
try testing.expectEqual(os.SIG.DFL, old_sa.handler.handler);
789-
// ensure we can ignore a signal
793+
794+
// Reinstall the signal w/o RESETHAND and re-raise
795+
sa.flags = os.SA.SIGINFO;
796+
try os.sigaction(os.SIG.USR1, &sa, null);
797+
try os.raise(os.SIG.USR1);
798+
try testing.expect(S.handler_called_count == 2);
799+
800+
// Now set the signal to ignored
790801
sa.handler = .{ .handler = os.SIG.IGN };
802+
sa.flags = 0;
791803
try os.sigaction(os.SIG.USR1, &sa, null);
792-
// and ensure it is now ignored
804+
805+
// Re-raise to ensure handler is actually ignored
793806
try os.raise(os.SIG.USR1);
794-
try testing.expect(signal_test_handler_called_count == 1);
795-
// and that ignored state is returned when querying
807+
try testing.expect(S.handler_called_count == 2);
808+
809+
// Ensure that ignored state is returned when querying
796810
try os.sigaction(os.SIG.USR1, null, &old_sa);
797811
try testing.expectEqual(os.SIG.IGN, old_sa.handler.handler.?);
798812
}

0 commit comments

Comments
 (0)