當前位置: 首頁>>代碼示例>>Golang>>正文


Golang context.TODO函數代碼示例

本文整理匯總了Golang中context.TODO函數的典型用法代碼示例。如果您正苦於以下問題:Golang TODO函數的具體用法?Golang TODO怎麽用?Golang TODO使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了TODO函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestHandlerPatchItemReplaceInvalidModifiedSinceDate

func TestHandlerPatchItemReplaceInvalidModifiedSinceDate(t *testing.T) {
	index := resource.NewIndex()
	s := mem.NewHandler()
	now := time.Now()
	s.Insert(context.TODO(), []*resource.Item{
		{ID: "1", Updated: now, Payload: map[string]interface{}{"id": "1"}},
	})
	test := index.Bind("test", schema.Schema{Fields: schema.Fields{"id": {}}}, s, resource.DefaultConf)
	r, _ := http.NewRequest("PATCH", "/test/1", bytes.NewBufferString(`{"id": "1"}`))
	r.Header.Set("If-Unmodified-Since", "invalid date")
	rm := &RouteMatch{
		ResourcePath: []*ResourcePathComponent{
			&ResourcePathComponent{
				Name:     "test",
				Field:    "id",
				Value:    "1",
				Resource: test,
			},
		},
	}
	status, _, body := itemPatch(context.TODO(), r, rm)
	assert.Equal(t, http.StatusBadRequest, status)
	if assert.IsType(t, body, &Error{}) {
		err := body.(*Error)
		assert.Equal(t, http.StatusBadRequest, err.Code)
		assert.Equal(t, "Invalid If-Unmodified-Since header", err.Message)
	}
}
開發者ID:rs,項目名稱:rest-layer,代碼行數:28,代碼來源:method_item_patch_test.go

示例2: TestHandlerPatchItemCannotChangeID

func TestHandlerPatchItemCannotChangeID(t *testing.T) {
	index := resource.NewIndex()
	s := mem.NewHandler()
	s.Insert(context.TODO(), []*resource.Item{
		{ID: "1", Payload: map[string]interface{}{"id": "1"}},
	})
	test := index.Bind("test", schema.Schema{Fields: schema.Fields{"id": {}}}, s, resource.DefaultConf)
	r, _ := http.NewRequest("PATCH", "/test/1", bytes.NewBufferString(`{"id": "2"}`))
	rm := &RouteMatch{
		ResourcePath: []*ResourcePathComponent{
			&ResourcePathComponent{
				Name:     "test",
				Field:    "id",
				Value:    "1",
				Resource: test,
			},
		},
	}
	status, headers, body := itemPatch(context.TODO(), r, rm)
	assert.Equal(t, 422, status)
	assert.Nil(t, headers)
	if assert.IsType(t, body, &Error{}) {
		err := body.(*Error)
		assert.Equal(t, 422, err.Code)
		assert.Equal(t, "Cannot change document ID", err.Message)
	}
}
開發者ID:rs,項目名稱:rest-layer,代碼行數:27,代碼來源:method_item_patch_test.go

示例3: TestNewWorkerPool

func TestNewWorkerPool(t *testing.T) {
	var count int64

	fn := func() {
		atomic.AddInt64(&count, 1)
	}

	p := concurrency.NewWorkerPool(context.TODO(), concurrency.Config{
		CloseTimeout: time.Millisecond * 200,
		Capacity:     16,
	})
	err := p.Open()
	assert.NoError(t, err)

	wg := &sync.WaitGroup{}
	for i := 0; i < 100; i++ {
		wg.Add(1)
		err = p.Execute(context.TODO(), func() {
			defer wg.Done()
			fn()
		})
	}

	wg.Wait()

	err = p.Close()
	assert.NoError(t, err)

	err = p.Wait()
	assert.NoError(t, err)

	assert.EqualValues(t, 100, count)

}
開發者ID:akaspin,項目名稱:concurrency,代碼行數:34,代碼來源:worker_test.go

示例4: TestHandlerPatchItemFound

func TestHandlerPatchItemFound(t *testing.T) {
	index := resource.NewIndex()
	s := mem.NewHandler()
	s.Insert(context.TODO(), []*resource.Item{
		{ID: "1", Payload: map[string]interface{}{"id": "1", "foo": "bar"}},
		{ID: "2", Payload: map[string]interface{}{"id": "2", "foo": "bar"}},
		{ID: "3", Payload: map[string]interface{}{"id": "3", "foo": "bar"}},
	})
	test := index.Bind("test", schema.Schema{Fields: schema.Fields{
		"id":  {},
		"foo": {},
	}}, s, resource.DefaultConf)
	r, _ := http.NewRequest("PATCH", "/test/2", bytes.NewBufferString(`{"id": "2", "foo": "baz"}`))
	rm := &RouteMatch{
		ResourcePath: []*ResourcePathComponent{
			&ResourcePathComponent{
				Name:     "test",
				Field:    "id",
				Value:    "2",
				Resource: test,
			},
		},
	}
	status, headers, body := itemPatch(context.TODO(), r, rm)
	assert.Equal(t, http.StatusOK, status)
	assert.Nil(t, headers)
	if assert.IsType(t, body, &resource.Item{}) {
		i := body.(*resource.Item)
		assert.Equal(t, "2", i.ID)
		assert.Equal(t, map[string]interface{}{"id": "2", "foo": "baz"}, i.Payload)
	}
}
開發者ID:rs,項目名稱:rest-layer,代碼行數:32,代碼來源:method_item_patch_test.go

示例5: TestHandlerPutItemReplaceModeNotAllowed

func TestHandlerPutItemReplaceModeNotAllowed(t *testing.T) {
	index := resource.NewIndex()
	s := mem.NewHandler()
	s.Insert(context.TODO(), []*resource.Item{
		{ID: "1", Payload: map[string]interface{}{"id": "1"}},
	})
	test := index.Bind("test", schema.Schema{Fields: schema.Fields{"id": {}}}, s, resource.Conf{
		AllowedModes: []resource.Mode{resource.Create},
	})
	r, _ := http.NewRequest("PUT", "/test/1", bytes.NewBufferString(`{}`))
	rm := &RouteMatch{
		ResourcePath: []*ResourcePathComponent{
			&ResourcePathComponent{
				Name:     "test",
				Field:    "id",
				Value:    "1",
				Resource: test,
			},
		},
	}
	status, headers, body := itemPut(context.TODO(), r, rm)
	assert.Equal(t, http.StatusMethodNotAllowed, status)
	assert.Nil(t, headers)
	if assert.IsType(t, body, &Error{}) {
		err := body.(*Error)
		assert.Equal(t, http.StatusMethodNotAllowed, err.Code)
		assert.Equal(t, "Invalid method", err.Message)
	}
}
開發者ID:rs,項目名稱:rest-layer,代碼行數:29,代碼來源:method_item_put_test.go

示例6: TestAlertingExecutor

func TestAlertingExecutor(t *testing.T) {
	Convey("Test alert execution", t, func() {
		handler := NewEvalHandler()

		Convey("Show return triggered with single passing condition", func() {
			context := NewEvalContext(context.TODO(), &Rule{
				Conditions: []Condition{&conditionStub{
					firing: true,
				}},
			})

			handler.Eval(context)
			So(context.Firing, ShouldEqual, true)
		})

		Convey("Show return false with not passing asdf", func() {
			context := NewEvalContext(context.TODO(), &Rule{
				Conditions: []Condition{
					&conditionStub{firing: true, matches: []*EvalMatch{&EvalMatch{}, &EvalMatch{}}},
					&conditionStub{firing: false},
				},
			})

			handler.Eval(context)
			So(context.Firing, ShouldEqual, false)
		})
	})
}
開發者ID:wk66,項目名稱:grafana,代碼行數:28,代碼來源:eval_handler_test.go

示例7: TestHandlerGetItem

func TestHandlerGetItem(t *testing.T) {
	s := mem.NewHandler()
	s.Insert(context.TODO(), []*resource.Item{
		{ID: "1", Payload: map[string]interface{}{"id": "1"}},
		{ID: "2", Payload: map[string]interface{}{"id": "2"}},
		{ID: "3", Payload: map[string]interface{}{"id": "3"}},
	})
	index := resource.NewIndex()
	test := index.Bind("test", schema.Schema{}, s, resource.DefaultConf)
	r, _ := http.NewRequest("GET", "/test/2", nil)
	rm := &RouteMatch{
		ResourcePath: []*ResourcePathComponent{
			&ResourcePathComponent{
				Name:     "test",
				Field:    "id",
				Value:    "2",
				Resource: test,
			},
		},
	}
	status, headers, body := itemGet(context.TODO(), r, rm)
	assert.Equal(t, http.StatusOK, status)
	assert.Nil(t, headers)
	if assert.IsType(t, body, &resource.Item{}) {
		i := body.(*resource.Item)
		assert.Equal(t, "2", i.ID)
	}
}
開發者ID:rs,項目名稱:rest-layer,代碼行數:28,代碼來源:method_item_get_test.go

示例8: TestHandlerDeleteItemEtag

func TestHandlerDeleteItemEtag(t *testing.T) {
	s := mem.NewHandler()
	s.Insert(context.TODO(), []*resource.Item{
		{ID: "1", ETag: "a", Payload: map[string]interface{}{"id": "1"}},
	})
	index := resource.NewIndex()
	test := index.Bind("test", schema.Schema{
		Fields: schema.Fields{"foo": {Filterable: true}},
	}, s, resource.DefaultConf)
	r, _ := http.NewRequest("DELETE", "/test/2", nil)
	r.Header.Set("If-Match", "a")
	rm := &RouteMatch{
		ResourcePath: []*ResourcePathComponent{
			&ResourcePathComponent{
				Name:     "test",
				Field:    "id",
				Value:    "1",
				Resource: test,
			},
		},
	}
	status, headers, body := itemDelete(context.TODO(), r, rm)
	assert.Equal(t, http.StatusNoContent, status)
	assert.Nil(t, headers)
	assert.Nil(t, body)

	l, err := s.Find(context.TODO(), resource.NewLookup(), 1, -1)
	assert.NoError(t, err)
	assert.Len(t, l.Items, 0)
}
開發者ID:rs,項目名稱:rest-layer,代碼行數:30,代碼來源:method_item_delete_test.go

示例9: TestHandlerGetItemModifiedDontMatch

func TestHandlerGetItemModifiedDontMatch(t *testing.T) {
	s := mem.NewHandler()
	now := time.Now()
	yesterday := now.Add(-24 * time.Hour)
	s.Insert(context.TODO(), []*resource.Item{
		{ID: "1", Updated: now, Payload: map[string]interface{}{"id": "1"}},
		{ID: "2", Updated: now, Payload: map[string]interface{}{"id": "2"}},
		{ID: "3", Updated: now, Payload: map[string]interface{}{"id": "3"}},
	})
	index := resource.NewIndex()
	test := index.Bind("test", schema.Schema{}, s, resource.DefaultConf)
	r, _ := http.NewRequest("GET", "/test/2", nil)
	r.Header.Set("If-Modified-Since", yesterday.Format(time.RFC1123))
	rm := &RouteMatch{
		ResourcePath: []*ResourcePathComponent{
			&ResourcePathComponent{
				Name:     "test",
				Field:    "id",
				Value:    "2",
				Resource: test,
			},
		},
	}
	status, headers, body := itemGet(context.TODO(), r, rm)
	assert.Equal(t, http.StatusOK, status)
	assert.Nil(t, headers)
	if assert.IsType(t, body, &resource.Item{}) {
		i := body.(*resource.Item)
		assert.Equal(t, "2", i.ID)
	}
}
開發者ID:rs,項目名稱:rest-layer,代碼行數:31,代碼來源:method_item_get_test.go

示例10: TestHandlerGetItemInvalidIfModifiedSince

func TestHandlerGetItemInvalidIfModifiedSince(t *testing.T) {
	s := mem.NewHandler()
	now := time.Now()
	s.Insert(context.TODO(), []*resource.Item{
		{ID: "1", Updated: now, Payload: map[string]interface{}{"id": "1"}},
		{ID: "2", Updated: now, Payload: map[string]interface{}{"id": "2"}},
		{ID: "3", Updated: now, Payload: map[string]interface{}{"id": "3"}},
	})
	index := resource.NewIndex()
	test := index.Bind("test", schema.Schema{}, s, resource.DefaultConf)
	r, _ := http.NewRequest("GET", "/test/2", nil)
	r.Header.Set("If-Modified-Since", "invalid date")
	rm := &RouteMatch{
		ResourcePath: []*ResourcePathComponent{
			&ResourcePathComponent{
				Name:     "test",
				Field:    "id",
				Value:    "2",
				Resource: test,
			},
		},
	}
	status, headers, body := itemGet(context.TODO(), r, rm)
	assert.Equal(t, http.StatusBadRequest, status)
	assert.Nil(t, headers)
	if assert.IsType(t, body, &Error{}) {
		err := body.(*Error)
		assert.Equal(t, http.StatusBadRequest, err.Code)
		assert.Equal(t, "Invalid If-Modified-Since header", err.Message)
	}
}
開發者ID:rs,項目名稱:rest-layer,代碼行數:31,代碼來源:method_item_get_test.go

示例11: TestHandlerGetItemEtagMatch

func TestHandlerGetItemEtagMatch(t *testing.T) {
	s := mem.NewHandler()
	s.Insert(context.TODO(), []*resource.Item{
		{ID: "1", ETag: "a", Payload: map[string]interface{}{"id": "1"}},
		{ID: "2", ETag: "a", Payload: map[string]interface{}{"id": "2"}},
		{ID: "3", ETag: "a", Payload: map[string]interface{}{"id": "3"}},
	})
	index := resource.NewIndex()
	test := index.Bind("test", schema.Schema{}, s, resource.DefaultConf)
	r, _ := http.NewRequest("GET", "/test/2", nil)
	r.Header.Set("If-None-Match", "a")
	rm := &RouteMatch{
		ResourcePath: []*ResourcePathComponent{
			&ResourcePathComponent{
				Name:     "test",
				Field:    "id",
				Value:    "2",
				Resource: test,
			},
		},
	}
	status, headers, body := itemGet(context.TODO(), r, rm)
	assert.Equal(t, http.StatusNotModified, status)
	assert.Nil(t, headers)
	assert.Nil(t, body)
}
開發者ID:rs,項目名稱:rest-layer,代碼行數:26,代碼來源:method_item_get_test.go

示例12: TestGitHubClient_Upload

func TestGitHubClient_Upload(t *testing.T) {
	client := testGithubClient(t)
	testTag := "github-client-upload-asset"
	req := &github.RepositoryRelease{
		TagName: github.String(testTag),
		Draft:   github.Bool(true),
	}

	release, err := client.CreateRelease(context.TODO(), req)
	if err != nil {
		t.Fatal("CreateRelease failed:", err)
	}

	defer func() {
		if err := client.DeleteRelease(context.TODO(), *release.ID); err != nil {
			t.Fatalf("DeleteRelease failed: %s", err)
		}
	}()

	filename := filepath.Join("./testdata", "darwin_386")
	asset, err := client.UploadAsset(context.TODO(), *release.ID, filename)
	if err != nil {
		t.Fatal("UploadAsset failed:", err)
	}

	githubClient, ok := client.(*GitHubClient)
	if !ok {
		t.Fatal("Faield to asset to GithubClient")
	}

	rc, url, err := githubClient.Repositories.DownloadReleaseAsset(
		githubClient.Owner, githubClient.Repo, *asset.ID)
	if err != nil {
		t.Fatal("DownloadReleaseAsset failed:", err)
	}

	var buf bytes.Buffer
	if len(url) != 0 {
		res, err := http.Get(url)
		if err != nil {
			t.Fatal("http.Get failed:", err)
		}

		if _, err := io.Copy(&buf, res.Body); err != nil {
			t.Fatal("Copy failed:", err)
		}
		res.Body.Close()

	} else {
		if _, err := io.Copy(&buf, rc); err != nil {
			t.Fatal("Copy failed:", err)
		}
		rc.Close()

	}

	if got, want := buf.String(), "darwin_386\n"; got != want {
		t.Fatalf("file body is %q, want %q", got, want)
	}
}
開發者ID:tchssk,項目名稱:ghr,代碼行數:60,代碼來源:github_test.go

示例13: TestHandlerGetListInvalidPage

func TestHandlerGetListInvalidPage(t *testing.T) {
	index := resource.NewIndex()
	test := index.Bind("test", schema.Schema{}, nil, resource.DefaultConf)
	r, _ := http.NewRequest("GET", "/test", nil)
	rm := &RouteMatch{
		ResourcePath: []*ResourcePathComponent{
			&ResourcePathComponent{
				Name:     "test",
				Resource: test,
			},
		},
		Params: url.Values{
			"page": []string{"invalid"},
		},
	}
	status, headers, body := listGet(context.TODO(), r, rm)
	assert.Equal(t, 422, status)
	assert.Nil(t, headers)
	if assert.IsType(t, body, &Error{}) {
		err := body.(*Error)
		assert.Equal(t, 422, err.Code)
		assert.Equal(t, "Invalid `page` parameter", err.Message)
	}

	rm.Params.Set("page", "-1")

	status, headers, body = listGet(context.TODO(), r, rm)
	assert.Equal(t, 422, status)
	assert.Nil(t, headers)
	if assert.IsType(t, body, &Error{}) {
		err := body.(*Error)
		assert.Equal(t, 422, err.Code)
		assert.Equal(t, "Invalid `page` parameter", err.Message)
	}
}
開發者ID:rs,項目名稱:rest-layer,代碼行數:35,代碼來源:method_get_test.go

示例14: TestHandlerDeleteList

func TestHandlerDeleteList(t *testing.T) {
	s := mem.NewHandler()
	s.Insert(context.TODO(), []*resource.Item{
		{ID: "1", Payload: map[string]interface{}{}},
		{ID: "2", Payload: map[string]interface{}{}},
		{ID: "3", Payload: map[string]interface{}{}},
		{ID: "4", Payload: map[string]interface{}{}},
		{ID: "5", Payload: map[string]interface{}{}},
	})
	index := resource.NewIndex()
	test := index.Bind("test", schema.Schema{}, s, resource.DefaultConf)
	r, _ := http.NewRequest("DELETE", "/test", bytes.NewBufferString("{}"))
	rm := &RouteMatch{
		ResourcePath: []*ResourcePathComponent{
			&ResourcePathComponent{
				Name:     "test",
				Resource: test,
			},
		},
	}
	status, headers, body := listDelete(context.TODO(), r, rm)
	assert.Equal(t, http.StatusNoContent, status)
	assert.Equal(t, http.Header{"X-Total": []string{"5"}}, headers)
	assert.Nil(t, body)

	l, err := s.Find(context.TODO(), resource.NewLookup(), 1, -1)
	assert.NoError(t, err)
	assert.Len(t, l.Items, 0)
}
開發者ID:rs,項目名稱:rest-layer,代碼行數:29,代碼來源:method_delete_test.go

示例15: TestHandlerGetListPagination

func TestHandlerGetListPagination(t *testing.T) {
	s := mem.NewHandler()
	s.Insert(context.TODO(), []*resource.Item{
		{ID: "1"},
		{ID: "2"},
		{ID: "3"},
		{ID: "4"},
		{ID: "5"},
	})
	index := resource.NewIndex()
	test := index.Bind("test", schema.Schema{}, s, resource.DefaultConf)
	r, _ := http.NewRequest("GET", "/test", nil)
	rm := &RouteMatch{
		ResourcePath: []*ResourcePathComponent{
			&ResourcePathComponent{
				Name:     "test",
				Resource: test,
			},
		},
		Params: url.Values{
			"page":  []string{"2"},
			"limit": []string{"2"},
		},
	}
	status, headers, body := listGet(context.TODO(), r, rm)
	assert.Equal(t, http.StatusOK, status)
	assert.Nil(t, headers)
	if assert.IsType(t, body, &resource.ItemList{}) {
		l := body.(*resource.ItemList)
		if assert.Len(t, l.Items, 2) {
			assert.Equal(t, "3", l.Items[0].ID)
			assert.Equal(t, "4", l.Items[1].ID)
		}
		assert.Equal(t, 2, l.Page)
		assert.Equal(t, 5, l.Total)
	}

	rm.Params.Set("page", "3")

	status, headers, body = listGet(context.TODO(), r, rm)
	assert.Equal(t, http.StatusOK, status)
	assert.Nil(t, headers)
	if assert.IsType(t, body, &resource.ItemList{}) {
		l := body.(*resource.ItemList)
		if assert.Len(t, l.Items, 1) {
			assert.Equal(t, "5", l.Items[0].ID)
		}
		assert.Equal(t, 3, l.Page)
		assert.Equal(t, 5, l.Total)
	}
}
開發者ID:rs,項目名稱:rest-layer,代碼行數:51,代碼來源:method_get_test.go


注:本文中的context.TODO函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。