Example #1
0
func TestNoTimeout(t *testing.T) {
	ctx := context.Background()
	resp, err := doRequest(ctx)

	if resp == nil || err != nil {
		t.Fatalf("error received from client: %v %v", err, resp)
	}
}
Example #2
0
func onetask() context.Context {
	l := list.New()
	l.PushBack("task one")

	ctx := context.WithValue(context.Background(), "tasks", l)
	ctx = context.WithValue(ctx, "logger", log.New(os.Stdout, "testing: ", log.LstdFlags))
	return ctx
}
Example #3
0
func ExampleWithTimeout() {
	// Pass a context with a timeout to tell a blocking function that it
	// should abandon its work after the timeout elapses.
	ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
	select {
	case <-time.After(200 * time.Millisecond):
		fmt.Println("overslept")
	case <-ctx.Done():
		fmt.Println(ctx.Err()) // prints "context deadline exceeded"
	}
	// Output:
	// context deadline exceeded
}
Example #4
0
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)
	}
}
Example #5
0
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)
	}
}
Example #6
0
func notasks() context.Context {
	ctx := context.WithValue(context.Background(), "tasks", list.New())
	ctx = context.WithValue(ctx, "logger", log.New(os.Stdout, "testing: ", log.LstdFlags))
	return ctx
}
Example #7
0
}

// appendItem adds a task to the Uber hypermedia document.
func (ud *udoc) appendItem(taskid, value string) {
	task := udata{ID: taskid,
		Rel:  []string{"item"},
		Name: "tasks",
		Data: []udata{
			udata{Rel: []string{"complete"}, URL: "/tasks/complete/", Model: fmt.Sprintf("id=%s", taskid), Action: "append"},
			udata{Name: "text", Value: value}}}

	ud.Uber.Data[1].Data = append(ud.Uber.Data[1].Data, task)
}

var (
	taskctx = context.Background()
)

func init() {
	taskctx = context.WithValue(taskctx, "tasks", list.New())
	taskctx = context.WithValue(taskctx, "logger", log.New(os.Stdout, "taskd: ", log.LstdFlags))
	http.Handle("/", handlers.CompressHandler(handlers.LoggingHandler(os.Stdout, router())))
}

func main() {
	http.ListenAndServe(":3006", nil)
}

func router() *mux.Router {
	r := mux.NewRouter()
	r.Handle("/tasks", http.Handler(ContextAdapter{ctx: taskctx, handler: ContextHandlerFunc(tasklist)})).Methods("GET")