From 5924cdac33161e7b4ee12e74e0505a3a5682ffe2 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sun, 19 Apr 2020 11:31:12 -0700 Subject: [PATCH] Add example tests --- example_test.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 example_test.go diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..10bf93c --- /dev/null +++ b/example_test.go @@ -0,0 +1,78 @@ +// Copyright 2020 Kentaro Hibino. All rights reserved. +// Use of this source code is governed by a MIT license +// that can be found in the LICENSE file. + +package asynq_test + +import ( + "log" + "os" + "os/signal" + + "github.com/hibiken/asynq" + "golang.org/x/sys/unix" +) + +func ExampleServer_Run() { + srv := asynq.NewServer( + asynq.RedisClientOpt{Addr: ":6379"}, + asynq.Config{Concurrency: 20}, + ) + + h := asynq.NewServeMux() + // ... Register handlers + + // Run blocks and waits for os signal to terminate the program. + if err := srv.Run(h); err != nil { + log.Fatal(err) + } +} + +func ExampleServer_Stop() { + srv := asynq.NewServer( + asynq.RedisClientOpt{Addr: ":6379"}, + asynq.Config{Concurrency: 20}, + ) + + h := asynq.NewServeMux() + // ... Register handlers + + if err := srv.Start(h); err != nil { + log.Fatal(err) + } + + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, unix.SIGTERM, unix.SIGINT) + <-sigs // wait for termination signal + + srv.Stop() +} + +func ExampleServer_Quiet() { + srv := asynq.NewServer( + asynq.RedisClientOpt{Addr: ":6379"}, + asynq.Config{Concurrency: 20}, + ) + + h := asynq.NewServeMux() + // ... Register handlers + + if err := srv.Start(h); err != nil { + log.Fatal(err) + } + + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, unix.SIGTERM, unix.SIGINT, unix.SIGTSTP) + // Handle SIGTERM, SIGINT to exit the program. + // Handle SIGTSTP to stop processing new tasks. + for { + s := <-sigs + if s == unix.SIGTSTP { + srv.Quiet() // stop processing new tasks + continue + } + break + } + + srv.Stop() +}