|
|
@@ -30,3 +30,56 @@ func TestContext(t *testing.T) {
|
|
|
|
|
|
time.Sleep(1 * time.Hour)
|
|
|
}
|
|
|
+
|
|
|
+func TestContextDone(t *testing.T) {
|
|
|
+ ctx, cancel := context.WithCancelCause(context.Background())
|
|
|
+
|
|
|
+ go func() {
|
|
|
+ for {
|
|
|
+ select {
|
|
|
+ case <-ctx.Done():
|
|
|
+ err := ctx.Err()
|
|
|
+ if err == context.Canceled {
|
|
|
+ err = context.Cause(ctx)
|
|
|
+ }
|
|
|
+ logger.Info("context done 1", err)
|
|
|
+ default:
|
|
|
+ }
|
|
|
+ time.Sleep(5 * time.Second)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ go func() {
|
|
|
+ for {
|
|
|
+ select {
|
|
|
+ case <-ctx.Done():
|
|
|
+ err := ctx.Err()
|
|
|
+ if err == context.Canceled {
|
|
|
+ err = context.Cause(ctx)
|
|
|
+ }
|
|
|
+ logger.Info("context done 2", err)
|
|
|
+ default:
|
|
|
+ }
|
|
|
+ time.Sleep(5 * time.Second)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ go func() {
|
|
|
+ for {
|
|
|
+ select {
|
|
|
+ case <-ctx.Done():
|
|
|
+ err := ctx.Err()
|
|
|
+ if err == context.Canceled {
|
|
|
+ err = context.Cause(ctx)
|
|
|
+ }
|
|
|
+ logger.Info("context done 3", err)
|
|
|
+ default:
|
|
|
+ }
|
|
|
+ time.Sleep(5 * time.Second)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ cancel(fmt.Errorf("%s", "cancel"))
|
|
|
+
|
|
|
+ time.Sleep(1 * time.Hour)
|
|
|
+}
|