func TestCancelAfterRequest(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())

	resp, err := doRequest(ctx)

	// Cancel before reading the body.
	// Request.Body should still be readable after the context is canceled.
	cancel()

	b, err := ioutil.ReadAll(resp.Body)
	if err != nil || string(b) != requestBody {
		t.Fatalf("could not read body: %q %v", b, err)
	}
}
Beispiel #2
0
func main() {
	// logging
	logrus.SetLevel(logrus.DebugLevel)

	// seed random
	rand.Seed(time.Now().Unix())

	//
	ctx, shutdown := context.WithCancel(context.Background())

	// dispatcher
	dispatch := dispatch.New()
	go dispatch.Run(ctx)

	// Asana
	team, err := strconv.Atoi(os.Getenv("ASANA_TEAM"))
	if err != nil {
		logrus.WithField("error", err).Fatal("could not convert ASANA_TEAM to int")
	}
	asana, err := asana.New(os.Getenv("ASANA_TOKEN"), team)
	if err != nil {
		logrus.WithField("error", err).Fatal("could not initialize Asana")
	}
	go asana.Handle(ctx, dispatch.Register("github:opened"))

	// Github
	github, err := github.New(os.Getenv("PORT"))
	if err != nil {
		logrus.WithField("error", err).Fatal("could not initialize Github")
	}
	go func() {
		err := github.Produce(dispatch.Send)
		if err != nil {
			logrus.WithField("error", err).Error("error starting producer")
		}
		logrus.Info("shutting down due to HTTP stopping")
		shutdown()
	}()

	// and finally sleep and catch events
	defer shutdown()
	go catch(shutdown)

	// give services time to finish and shut down
	<-ctx.Done()
	logrus.Info("waiting for grace period for services to shut down")
	time.Sleep(time.Second * 5)
}
func TestCancel(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	go func() {
		time.Sleep(requestDuration / 2)
		cancel()
	}()

	resp, err := doRequest(ctx)

	if resp != nil || err == nil {
		t.Fatalf("expected error, didn't get one. resp: %v", resp)
	}
	if err != ctx.Err() {
		t.Fatalf("expected error from context but got: %v", err)
	}
}