context_breaker.mx raw

   1  // context is live stdlib code - net's dial/DNS timeouts, net/http's request
   2  // cancellation and crypto/tls's connection close all run through it - but no
   3  // repository imports it and nothing exercised it. This pins the breaker
   4  // semantics: Done/Err/Cause, the deadline timer, AfterFunc, and WithoutCancel.
   5  package main
   6  
   7  import (
   8  	"context"
   9  	"errors"
  10  	"time"
  11  )
  12  
  13  // hit records that the AfterFunc callback ran. A Moxie func value carries no
  14  // environment, so the callback is a type with a Call method.
  15  type hit struct {
  16  	ran bool
  17  }
  18  
  19  func (h *hit) Call() {
  20  	h.ran = true
  21  }
  22  
  23  func main() {
  24  	// Termination: a batch program must still be stoppable, so the entry
  25  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  26  	select {
  27  	case <-shutdown:
  28  		return
  29  	default:
  30  	}
  31  
  32  	bg := context.Background()
  33  	if bg.Done() != nil {
  34  		panic("Background().Done() must be nil")
  35  	}
  36  	if bg.Err() != nil {
  37  		panic("Background().Err() must be nil")
  38  	}
  39  
  40  	// WithCancel closes Done, sets Err, and runs a registered AfterFunc.
  41  	ctx, cancel := context.WithCancel(bg)
  42  	h1 := &hit{}
  43  	stop1 := context.AfterFunc(ctx, h1)
  44  	if h1.ran {
  45  		panic("AfterFunc ran before cancel")
  46  	}
  47  	cancel.Cancel()
  48  	if ctx.Err() != context.Canceled {
  49  		panic("cancel must set Err to Canceled")
  50  	}
  51  	if context.Cause(ctx) != context.Canceled {
  52  		panic("Cause of a plain cancel must be Canceled")
  53  	}
  54  	if !h1.ran {
  55  		panic("AfterFunc must run when the context is canceled")
  56  	}
  57  	if stop1.Stop() {
  58  		panic("Stop must report false once the callback has run")
  59  	}
  60  
  61  	// Stop before cancel prevents the callback.
  62  	ctx2, cancel2 := context.WithCancel(bg)
  63  	h2 := &hit{}
  64  	stop2 := context.AfterFunc(ctx2, h2)
  65  	if !stop2.Stop() {
  66  		panic("Stop must report true before the callback runs")
  67  	}
  68  	cancel2.Cancel()
  69  	if h2.ran {
  70  		panic("Stop must prevent the callback from running")
  71  	}
  72  
  73  	// WithCancelCause records the cause, which Cause returns instead of Canceled.
  74  	sentinel := errors.New("sentinel")
  75  	ctx3, cancel3 := context.WithCancelCause(bg)
  76  	cancel3.Cancel(sentinel)
  77  	if context.Cause(ctx3) != sentinel {
  78  		panic("Cause must return the cancellation cause")
  79  	}
  80  
  81  	// Cancellation propagates from parent to child.
  82  	parent, cancelParent := context.WithCancel(bg)
  83  	child, cancelChild := context.WithCancel(parent)
  84  	cancelParent.Cancel()
  85  	if child.Err() != context.Canceled {
  86  		panic("canceling the parent must cancel the child")
  87  	}
  88  	cancelChild.Cancel()
  89  
  90  	// WithoutCancel is never canceled, even when its parent is.
  91  	detached := context.WithoutCancel(parent)
  92  	if detached.Done() != nil || detached.Err() != nil {
  93  		panic("WithoutCancel must not observe the parent's cancellation")
  94  	}
  95  
  96  	// WithTimeout fires on its own. Moxie has no goroutines: one package-level
  97  	// timer is re-armed to the earliest deadline, so this also exercises the
  98  	// scheduler.
  99  	tctx, tcancel := context.WithTimeout(bg, 10*time.Millisecond)
 100  	for i := int32(0); i < 2000 && tctx.Err() == nil; i++ {
 101  		time.Sleep(2 * time.Millisecond)
 102  	}
 103  	if tctx.Err() != context.DeadlineExceeded() {
 104  		panic("WithTimeout must expire with DeadlineExceeded")
 105  	}
 106  	if context.Cause(tctx) != context.DeadlineExceeded() {
 107  		panic("Cause of an expired deadline must be DeadlineExceeded")
 108  	}
 109  	tcancel.Cancel()
 110  
 111  	// An already-passed deadline cancels immediately, without a timer.
 112  	pctx, pcancel := context.WithDeadline(bg, time.Now().Add(-time.Second))
 113  	if pctx.Err() != context.DeadlineExceeded() {
 114  		panic("a past deadline must cancel immediately")
 115  	}
 116  	pcancel.Cancel()
 117  }
 118