From 80fb5ebddf380fed64fce8414db3a985a1e7956e Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Thu, 2 Dec 2021 06:31:37 -0800 Subject: [PATCH] Clarify the signals example slightly Based on feedback in #402 --- examples/signals/signals.go | 17 +++++++++------ examples/signals/signals.hash | 4 ++-- examples/testing/main_test.go | 6 ++++++ examples/testing/testing.hash | 4 ++-- public/signals | 40 ++++++++++++++++++++++++++++------- public/testing | 21 +++++++++++++++--- 6 files changed, 71 insertions(+), 21 deletions(-) diff --git a/examples/signals/signals.go b/examples/signals/signals.go index 4cd373b..1fd225b 100644 --- a/examples/signals/signals.go +++ b/examples/signals/signals.go @@ -18,19 +18,24 @@ func main() { // Go signal notification works by sending `os.Signal` // values on a channel. We'll create a channel to - // receive these notifications (we'll also make one to - // notify us when the program can exit). + // receive these notifications. Note that this channel + // should be buffered. sigs := make(chan os.Signal, 1) - done := make(chan bool, 1) // `signal.Notify` registers the given channel to // receive notifications of the specified signals. signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - // This goroutine executes a blocking receive for - // signals. When it gets one it'll print it out - // and then notify the program that it can finish. + // We could receive from `sigs` here in the main + // function, but let's see how this could also be + // done in a separate goroutine, to demonstrate + // a more realistic scenario of graceful shutdown. + done := make(chan bool, 1) + go func() { + // This goroutine executes a blocking receive for + // signals. When it gets one it'll print it out + // and then notify the program that it can finish. sig := <-sigs fmt.Println() fmt.Println(sig) diff --git a/examples/signals/signals.hash b/examples/signals/signals.hash index 205ac69..3735e8f 100644 --- a/examples/signals/signals.hash +++ b/examples/signals/signals.hash @@ -1,2 +1,2 @@ -ccee3fe41771b7cf56d64de38b12588022458154 -YRV64KEXJW1 +cd15508731199185f3205692af0f80cbdee4fcd7 +LauPuRo3v9l diff --git a/examples/testing/main_test.go b/examples/testing/main_test.go index e409d9d..25531da 100644 --- a/examples/testing/main_test.go +++ b/examples/testing/main_test.go @@ -66,3 +66,9 @@ func TestIntMinTableDriven(t *testing.T) { }) } } + +func BenchmarkIntMin(b *testing.B) { + for i := 0; i < b.N; i++ { + IntMin(1, 2) + } +} diff --git a/examples/testing/testing.hash b/examples/testing/testing.hash index 6dc53a9..4572b51 100644 --- a/examples/testing/testing.hash +++ b/examples/testing/testing.hash @@ -1,2 +1,2 @@ -c9ca6b71d9f762b689f1f08a490d8c7f7764fcb3 -vY8PN0c6BSx +25e8941d63b555a590e6d44a95ae0e41ecadadca +ALL2BVLkYEr diff --git a/public/signals b/public/signals index da60f15..c2ae6e2 100644 --- a/public/signals +++ b/public/signals @@ -46,7 +46,7 @@ Here’s how to handle signals in Go with channels.

- +
package main
 
@@ -83,15 +83,14 @@ Here’s how to handle signals in Go with channels.

Go signal notification works by sending os.Signal values on a channel. We’ll create a channel to -receive these notifications (we’ll also make one to -notify us when the program can exit).

+receive these notifications. Note that this channel +should be buffered.

     sigs := make(chan os.Signal, 1)
-    done := make(chan bool, 1)
 
@@ -110,6 +109,22 @@ receive notifications of the specified signals.

+ + +

We could receive from sigs here in the main +function, but let’s see how this could also be +done in a separate goroutine, to demonstrate +a more realistic scenario of graceful shutdown.

+ + + + +
+    done := make(chan bool, 1)
+
+ + +

This goroutine executes a blocking receive for @@ -119,9 +134,18 @@ and then notify the program that it can finish.

-
-    go func() {
-        sig := <-sigs
+          
    go func() {
+
+ + + + + + + + + +
        sig := <-sigs
         fmt.Println()
         fmt.Println(sig)
         done <- true
@@ -186,7 +210,7 @@ causing the program to print interrupt and then exit.

diff --git a/public/testing b/public/testing index 8a66507..afb9fa4 100644 --- a/public/testing +++ b/public/testing @@ -47,7 +47,7 @@ typically lives in the same package as the code it tests.

- +
 package main
 
@@ -167,7 +167,7 @@ when executing go test -v.

- +
        testname := fmt.Sprintf("%d,%d", tt.a, tt.b)
         t.Run(testname, func(t *testing.T) {
@@ -178,6 +178,21 @@ when executing go test -v.

}) } } +
+ + + + + + + + + +
func BenchmarkIntMin(b *testing.B) {
+    for i := 0; i < b.N; i++ {
+        IntMin(1, 2)
+    }
+}
 
@@ -229,7 +244,7 @@ when executing go test -v.