| | |
| | |
| | |
| |
|
| | package synctest_test |
| |
|
| | import ( |
| | "bufio" |
| | "bytes" |
| | "context" |
| | "io" |
| | "net" |
| | "net/http" |
| | "strings" |
| | "testing" |
| | "testing/synctest" |
| | "time" |
| | ) |
| |
|
| | |
| |
|
| | func TestTime(t *testing.T) { |
| | synctest.Test(t, func(t *testing.T) { |
| | start := time.Now() |
| | go func() { |
| | time.Sleep(1 * time.Nanosecond) |
| | t.Log(time.Since(start)) |
| | }() |
| | time.Sleep(2 * time.Nanosecond) |
| | t.Log(time.Since(start)) |
| | }) |
| | } |
| |
|
| | func TestWait(t *testing.T) { |
| | synctest.Test(t, func(t *testing.T) { |
| | done := false |
| | go func() { |
| | done = true |
| | }() |
| | |
| | synctest.Wait() |
| | t.Log(done) |
| | }) |
| | } |
| |
|
| | func TestContextAfterFunc(t *testing.T) { |
| | synctest.Test(t, func(t *testing.T) { |
| | |
| | ctx, cancel := context.WithCancel(t.Context()) |
| |
|
| | |
| | |
| | afterFuncCalled := false |
| | context.AfterFunc(ctx, func() { |
| | afterFuncCalled = true |
| | }) |
| |
|
| | |
| | synctest.Wait() |
| | if afterFuncCalled { |
| | t.Fatalf("before context is canceled: AfterFunc called") |
| | } |
| |
|
| | |
| | |
| | cancel() |
| | synctest.Wait() |
| | if !afterFuncCalled { |
| | t.Fatalf("after context is canceled: AfterFunc not called") |
| | } |
| | }) |
| | } |
| |
|
| | func TestContextWithTimeout(t *testing.T) { |
| | synctest.Test(t, func(t *testing.T) { |
| | |
| | const timeout = 5 * time.Second |
| | ctx, cancel := context.WithTimeout(t.Context(), timeout) |
| | defer cancel() |
| |
|
| | |
| | time.Sleep(timeout - time.Nanosecond) |
| | synctest.Wait() |
| | if err := ctx.Err(); err != nil { |
| | t.Fatalf("before timeout: ctx.Err() = %v, want nil\n", err) |
| | } |
| |
|
| | |
| | time.Sleep(time.Nanosecond) |
| | synctest.Wait() |
| | if err := ctx.Err(); err != context.DeadlineExceeded { |
| | t.Fatalf("after timeout: ctx.Err() = %v, want DeadlineExceeded\n", err) |
| | } |
| | }) |
| | } |
| |
|
| | func TestHTTPTransport100Continue(t *testing.T) { |
| | synctest.Test(t, func(*testing.T) { |
| | |
| | |
| | |
| | |
| | srvConn, cliConn := net.Pipe() |
| | defer cliConn.Close() |
| | defer srvConn.Close() |
| |
|
| | tr := &http.Transport{ |
| | |
| | DialContext: func(ctx context.Context, network, address string) (net.Conn, error) { |
| | return cliConn, nil |
| | }, |
| | |
| | ExpectContinueTimeout: 5 * time.Second, |
| | } |
| |
|
| | |
| | |
| | body := "request body" |
| | go func() { |
| | req, _ := http.NewRequest("PUT", "http://test.tld/", strings.NewReader(body)) |
| | req.Header.Set("Expect", "100-continue") |
| | resp, err := tr.RoundTrip(req) |
| | if err != nil { |
| | t.Errorf("RoundTrip: unexpected error %v\n", err) |
| | } else { |
| | resp.Body.Close() |
| | } |
| | }() |
| |
|
| | |
| | req, err := http.ReadRequest(bufio.NewReader(srvConn)) |
| | if err != nil { |
| | t.Fatalf("ReadRequest: %v\n", err) |
| | } |
| |
|
| | |
| | |
| | |
| | var gotBody bytes.Buffer |
| | go io.Copy(&gotBody, req.Body) |
| | synctest.Wait() |
| | if got, want := gotBody.String(), ""; got != want { |
| | t.Fatalf("before sending 100 Continue, read body: %q, want %q\n", got, want) |
| | } |
| |
|
| | |
| | |
| | srvConn.Write([]byte("HTTP/1.1 100 Continue\r\n\r\n")) |
| | synctest.Wait() |
| | if got, want := gotBody.String(), body; got != want { |
| | t.Fatalf("after sending 100 Continue, read body: %q, want %q\n", got, want) |
| | } |
| |
|
| | |
| | srvConn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n")) |
| |
|
| | |
| | |
| | }) |
| | } |
| |
|