-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathserver_test.go
More file actions
51 lines (46 loc) · 1.34 KB
/
server_test.go
File metadata and controls
51 lines (46 loc) · 1.34 KB
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
package workgroup_test
import (
"context"
"fmt"
"net/http"
"time"
"github.com/da440dil/go-workgroup"
)
func ExampleServer() {
srv := http.Server{Addr: "127.0.0.1:8080"}
// Create context to cancel execution after 100 milliseconds,
// increase timeout to manually interrupt execution with SIGINT
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
// Execute each function in its own goroutine
err := workgroup.Run(
workgroup.Server(
func() error {
fmt.Printf("Server listen at %v\n", srv.Addr)
err := srv.ListenAndServe()
fmt.Printf("Server stopped listening with error: %v\n", err)
if err != http.ErrServerClosed {
return err
}
return nil
},
func() error {
fmt.Println("Server is about to shutdown")
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
err := srv.Shutdown(ctx)
fmt.Printf("Server shutdown with error: %v\n", err)
return err
},
),
workgroup.Context(ctx),
workgroup.Signal(),
)
fmt.Printf("Workgroup run stopped with error: %v\n", err)
// Output:
// Server listen at 127.0.0.1:8080
// Server is about to shutdown
// Server stopped listening with error: http: Server closed
// Server shutdown with error: <nil>
// Workgroup run stopped with error: context deadline exceeded
}