예제 #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)
	}
}
예제 #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
}
예제 #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
}
예제 #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)
	}
}
예제 #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)
	}
}
예제 #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
}
예제 #7
0
파일: main.go 프로젝트: wkharold/uber-apps
}

// 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")