func TestNewRequestGET(t *testing.T) { // First call returns HTTP 500, second succeeds. serverCalls := 0 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { serverCalls++ content, err := ioutil.ReadAll(r.Body) ut.ExpectEqual(t, nil, err) ut.ExpectEqual(t, []byte{}, content) if serverCalls == 1 { w.WriteHeader(500) } else { fmt.Fprintf(w, "Hello, client\n") } })) defer ts.Close() httpReq, err := http.NewRequest("GET", ts.URL, nil) ut.AssertEqual(t, nil, err) clientCalls := 0 clientReq, err := NewRequest(http.DefaultClient, httpReq, func(resp *http.Response) error { clientCalls++ content, err := ioutil.ReadAll(resp.Body) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, "Hello, client\n", string(content)) ut.AssertEqual(t, nil, resp.Body.Close()) return nil }) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, nil, fast.Do(clientReq)) ut.AssertEqual(t, 200, clientReq.Status()) ut.AssertEqual(t, 2, serverCalls) ut.AssertEqual(t, 1, clientCalls) }
func TestRemoveAllMissing(t *testing.T) { td, err := ioutil.TempDir("", "pre-commit-go") ut.ExpectEqual(t, nil, err) foo := filepath.Join(td, "foo") err = ioutil.WriteFile(foo, []byte("yo"), 0600) ut.ExpectEqual(t, nil, err) ut.AssertEqual(t, nil, RemoveAll(td)) }
func TestCancelable(t *testing.T) { t.Parallel() c := newCanceler() ut.AssertEqual(t, nil, c.CancelationReason()) select { case <-c.Channel(): t.FailNow() default: } wg := sync.WaitGroup{} wg.Add(1) go func() { defer wg.Done() select { case err, isCanceled := <-c.Channel(): ut.ExpectEqualf(t, true, isCanceled, "Closed, but shouldn't be.") ut.ExpectEqual(t, ErrCanceled, err) } }() c.Cancel(nil) ut.AssertEqual(t, ErrCanceled, c.CancelationReason()) t.Log("waiting for goroutine above to end.") wg.Wait() ut.AssertEqual(t, nil, c.Close()) assertClosed(t, c) }
func TestGetJSONBadContentTypeIgnore(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, err := io.WriteString(w, "{}") ut.ExpectEqual(t, nil, err) })) defer ts.Close() status, err := GetJSON(fast, http.DefaultClient, ts.URL, nil) ut.AssertEqual(t, errors.New("unexpected Content-Type, expected \"application/json; charset=utf-8\", got \"text/plain; charset=utf-8\""), err) ut.AssertEqual(t, 200, status) }
// handlerJSON converts a jsonAPI http handler to a proper http.Handler. func handlerJSON(t *testing.T, handler jsonAPI) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { //ut.ExpectEqual(t, jsonContentType, r.Header.Get("Content-Type")) defer r.Body.Close() out := handler(r.Body) if out == nil { w.WriteHeader(500) } else { w.Header().Set("Content-Type", jsonContentType) ut.ExpectEqual(t, nil, json.NewEncoder(w).Encode(out)) } }) }
func TestGetJSON(t *testing.T) { // First call returns HTTP 500, second succeeds. serverCalls := 0 ts := httptest.NewServer(handlerJSON(t, func(body io.Reader) interface{} { serverCalls++ content, err := ioutil.ReadAll(body) ut.ExpectEqual(t, nil, err) ut.ExpectEqual(t, []byte{}, content) if serverCalls == 1 { return nil } return map[string]string{"success": "yeah"} })) defer ts.Close() actual := map[string]string{} status, err := GetJSON(fast, http.DefaultClient, ts.URL, &actual) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, 200, status) ut.AssertEqual(t, map[string]string{"success": "yeah"}, actual) ut.AssertEqual(t, 2, serverCalls) }
func TestCoveragePrerequisites(t *testing.T) { // This test can't be parallel. if !IsContinuousIntegration() { old := os.Getenv("CI") defer func() { ut.ExpectEqual(t, nil, os.Setenv("CI", old)) }() ut.AssertEqual(t, nil, os.Setenv("CI", "true")) ut.AssertEqual(t, true, IsContinuousIntegration()) } c := Coverage{UseCoveralls: true} ut.AssertEqual(t, 1, len(c.GetPrerequisites())) }
func TestPostJSON(t *testing.T) { // First call returns HTTP 500, second succeeds. serverCalls := 0 ts := httptest.NewServer(handlerJSON(t, func(body io.Reader) interface{} { serverCalls++ data := map[string]string{} ut.ExpectEqual(t, nil, json.NewDecoder(body).Decode(&data)) ut.ExpectEqual(t, map[string]string{"in": "all"}, data) if serverCalls == 1 { return nil } return map[string]string{"success": "yeah"} })) defer ts.Close() in := map[string]string{"in": "all"} actual := map[string]string{} status, err := PostJSON(fast, http.DefaultClient, ts.URL, in, &actual) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, 200, status) ut.AssertEqual(t, map[string]string{"success": "yeah"}, actual) ut.AssertEqual(t, 2, serverCalls) }
func TestGetJSONBadResultIgnore(t *testing.T) { serverCalls := 0 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { serverCalls++ w.Header().Set("Content-Type", jsonContentType) _, err := io.WriteString(w, "yo") ut.ExpectEqual(t, nil, err) })) defer ts.Close() status, err := GetJSON(fast, http.DefaultClient, ts.URL, nil) ut.AssertEqual(t, retry.Error{errors.New("bad response " + ts.URL + ": invalid character 'y' looking for beginning of value")}, err) ut.AssertEqual(t, 200, status) }
func TestCancelableUnblockAfterClosed(t *testing.T) { t.Parallel() c := newCanceler() wg := sync.WaitGroup{} wg.Add(1) go func() { defer wg.Done() select { case _, stillOpen := <-c.Channel(): ut.ExpectEqual(t, false, stillOpen) } }() ut.AssertEqual(t, nil, c.Close()) t.Log("waiting for goroutine above to end.") wg.Wait() assertClosed(t, c) }
func TestGoroutinePriorityPoolWithPriority(t *testing.T) { t.Parallel() const MAX_PRIORITIES = 15 pool := NewGoroutinePriorityPool(1, NewCanceler()) logs := make(chan int) wg := sync.WaitGroup{} for i := 0; i < MAX_PRIORITIES; i++ { wg.Add(1) i := i go func() { pool.Schedule(int64(i), func() { logs <- i }, nil) wg.Done() }() } wg.Wait() var fail error go func() { defer close(logs) fail = pool.Wait() ut.ExpectEqual(t, nil, fail) }() doneJobs := make([]bool, MAX_PRIORITIES) // First job can be any, the rest must be in order. prio := <-logs doneJobs[prio] = true for prio := range logs { ut.AssertEqual(t, false, doneJobs[prio]) doneJobs[prio] = true // All higher priority jobs must be finished. for p := 0; p < prio; p++ { ut.AssertEqual(t, true, doneJobs[prio]) } } for p, d := range doneJobs { ut.AssertEqualIndex(t, p, true, d) } }