func TestThrottle(t *testing.T) { l, err := net.Listen("tcp", "") if err != nil { t.Critical("could not initialize listener: %v", err) } throttled := NewThrottledListener(l, 10, 3) go http.Serve(throttled, nil) start := time.Now() var wg sync.WaitGroup for i := 1; i <= 3; i++ { conn, err := net.Dial("tcp", l.Addr().String()) if err != nil { t.Error(err) } wg.Add(1) go func() { defer wg.Done() defer conn.Close() if _, err = conn.Write([]byte("hello\n\n")); err != nil { t.Error(err) } b := make([]byte, 1000) if _, err = conn.Read(b); err != nil { t.Error(err) } }() } wg.Wait() diff := time.Now().Sub(start) if diff < 300*time.Millisecond { t.Error("want >= 300ms, got %v", diff) } l.Close() }
func TestPublished(t *testing.T) { l, err := Listen("") if err != nil { t.Critical("could not initialize listener: %v", err) } go http.Serve(l, nil) for i := 1; i <= 3; i++ { resp, err := http.Get(fmt.Sprintf("http://%s/debug/vars", l.Addr().String())) if err != nil { t.Fatal(err) } val, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatal(err) } http.DefaultTransport.(*http.Transport).CloseIdleConnections() vars := make(map[string]interface{}) err = json.Unmarshal(val, &vars) if err != nil { t.Error(err) } if vars["ConnCount"].(float64) != 1 { t.Errorf("want 1, got %v", vars["connection-count"]) } if vars["ConnAccepted"].(float64) != float64(i) { t.Errorf("want %d, got %v", i, vars["connection-count"]) } } l.Close() }
func testLaunch(t *testing.T) { var err error l, err := net.Listen("tcp", "") if err != nil { t.Critical("could not initialize listener: %v", err) } hostport := l.Addr().String() l.Close() _, port, err := net.SplitHostPort(hostport) if err != nil { t.Fatal(err) } cmd1 := launchServer(t, port, 1) defer cmd1.Process.Kill() testPid(t, port, cmd1.Process.Pid) cmd2 := launchServer(t, port, 2) defer cmd2.Process.Kill() err = cmd1.Wait() if err != nil { t.Error(err) } testPid(t, port, cmd2.Process.Pid) err = syscall.Kill(cmd2.Process.Pid, syscall.SIGTERM) if err != nil { t.Error(err) } err = cmd2.Wait() if err != nil { t.Error(err) } }
func TestCondSignalGenerations(t *testing.T) { var m sync.Mutex c := NewCond(&m) n := 100 running := make(chan bool, n) awake := make(chan int, n) for i := 0; i < n; i++ { go func(i int) { m.Lock() running <- true c.Wait() awake <- i m.Unlock() }(i) if i > 0 { a := <-awake if a != i-1 { t.Critical("wrong goroutine woke up: want %d, got %d", i-1, a) } } <-running m.Lock() c.Signal() m.Unlock() } }
func testServer(t *testing.T, want syscall.Signal) { l, err := Listen(os.Getenv("PORT")) if err != nil { t.Critical("could not initialize listener: %v", err) } go http.Serve(l, nil) got := Wait() l.Close() if want != got { t.Errorf("want %v, got %v", want, got) } }
func testPid(t *testing.T, port string, want int) { var resp *http.Response var err error for i := 0; i < 20; i++ { resp, err = http.Get(fmt.Sprintf("http://localhost:%s%s", port, pidURL)) if err != nil { if i == 19 { t.Fatal(err) } if strings.Contains(err.Error(), "connection refused") { time.Sleep(1000 * time.Millisecond) continue } t.Critical("unexpected error on port %v: %v", port, err) } break } num, err := ioutil.ReadAll(resp.Body) resp.Body.Close() if err != nil { t.Critical("could not read pid: %vd", err) } got, err := strconv.Atoi(string(num)) if err != nil { t.Critical("could not read pid: %vd", err) } if want != got { t.Errorf("want %d, got %d", want, got) } }
func TestBalancerConfiguration(t *testing.T) { configure := InitDldbConfiguration("") receiveBalancer := configure.BalancersConfiguration.GetBalancerConfiguration("engine") if receiveBalancer.InitRoutineNum != 500 { t.Critical("receive balancer read error, initroutineNum = %d, want %d", receiveBalancer.InitRoutineNum, 500) } if receiveBalancer.ManagerAutoDetect { t.Critical("receive balancer read error, ManagerAutoDetect is true, want false") } if configure.ServerConfiguration.Addr != "localhost" { t.Critical("server configuration init err!") } }
func TestTypes(t *testing.T) { in := make(map[string]interface{}) in["bytes"] = []byte("bytes") in["float64"] = float64(64) in["string"] = "string" in["bool"] = true in["time"] = time.Unix(1136243045, 0) in["int32"] = int32(-0x80000000) in["int"] = int(-0x80000000) in["int64"] = int64(-0x8000000000000000) in["uint"] = uint(0xFFFFFFFF) in["uint32"] = uint32(0xFFFFFFFF) in["uint64"] = uint64(0xFFFFFFFFFFFFFFFF) in["slice"] = []interface{}{1, nil} in["nil"] = nil encoded := VerifyMarshal(t, in) out := make(map[string]interface{}) err := Unmarshal(encoded, &out) if err != nil { t.Critical("unmarshal fail: %v\n", err) } if string(in["bytes"].([]byte)) != "bytes" { t.Error("bytes fail") } if out["float64"].(float64) != float64(64) { t.Error("float fail") } if string(out["string"].([]byte)) != "string" { t.Error("string fail") } if out["bool"].(bool) == false { t.Error("bool fail") } tm, ok := out["time"].(time.Time) if !ok { t.Error("time type failed") } if tm.Unix() != 1136243045 { t.Error("time failed") } if v := out["int32"].(int32); v != int32(-0x80000000) { t.Error("int32 fail: %v", v) } if v := out["int"].(int64); v != int64(-0x80000000) { t.Error("int fail: %v", v) } if v := out["int64"].(int64); v != int64(-0x8000000000000000) { t.Error("int64 fail: %v", v) } if v := out["uint"].(uint64); v != uint64(0xFFFFFFFF) { t.Error("uint fail: %v", v) } if v := out["uint32"].(uint64); v != uint64(0xFFFFFFFF) { t.Error("uint32 fail: %v", v) } if v := out["uint64"].(uint64); v != uint64(0xFFFFFFFFFFFFFFFF) { t.Error("uint64 fail: %v", v) } if v := out["slice"].([]interface{})[0].(int64); v != 1 { t.Error("slice fail: %v", v) } if v := out["slice"].([]interface{})[1]; v != nil { t.Error("slice fail: %v", v) } if nilval, ok := out["nil"]; !ok || nilval != nil { t.Error("nil fail") } }
// TestCustom tests custom unmarshalling func TestCustom(t *testing.T) { a := alltypes{ Bytes: []byte("bytes"), Float64: float64(64), String: "string", Bool: true, Time: time.Unix(1136243045, 0), Int32: int32(-0x80000000), Int: int(-0x80000000), Int64: int64(-0x8000000000000000), Uint64: uint64(0xFFFFFFFFFFFFFFFF), Strings: []string{"a", "b"}, Nil: nil, } encoded := VerifyMarshal(t, a) var out alltypes err := Unmarshal(encoded, &out) if err != nil { t.Critical("unmarshal fail: %v\n", err) } if string(out.Bytes) != "bytes" { t.Error("bytes fail: %s", out.Bytes) } if out.Float64 != 64 { t.Error("float fail: %v", out.Float64) } if out.String != "string" { t.Error("string fail: %v", out.String) } if !out.Bool { t.Error("bool fail: %v", out.Bool) } if out.Time.Unix() != 1136243045 { t.Error("time fail: %v", out.Time) } if out.Int32 != -0x80000000 { t.Error("int32 fail: %v", out.Int32) } if out.Int != -0x80000000 { t.Error("int fail: %v", out.Int) } if out.Int64 != -0x8000000000000000 { t.Error("int64 fail: %v", out.Int64) } if out.Uint64 != 0xFFFFFFFFFFFFFFFF { t.Error("uint64 fail: %v", out.Uint64) } if out.Strings[0] != "a" || out.Strings[1] != "b" { t.Error("strings fail: %v", out.Strings) } b := alltypes{Bytes: []byte(""), Strings: []string{"a"}} encoded = VerifyMarshal(t, b) var outb alltypes err = Unmarshal(encoded, &outb) if err != nil { t.Critical("unmarshal fail: %v\n", err) } if outb.Bytes == nil || len(outb.Bytes) != 0 { t.Error("nil bytes fail: %s", outb.Bytes) } }
func TestServer(t *testing.T) { type addResp struct { Id interface{} `json:"id"` Result Reply `json:"result"` Error interface{} `json:"error"` } cli, srv := net.Pipe() defer cli.Close() go ServeConn(srv) dec := json.NewDecoder(cli) // Send hand-coded requests to server, parse responses. for i := 0; i < 10; i++ { fmt.Fprintf(cli, `{"method": "Arith.Add", "id": "\u%04d", "params": [{"A": %d, "B": %d}]}`, i, i, i+1) var resp addResp err := dec.Decode(&resp) if err != nil { t.Critical("Decode: %s", err) } if resp.Error != nil { t.Critical("resp.Error: %s", resp.Error) } if resp.Id.(string) != string(i) { t.Critical("resp: bad id %q want %q", resp.Id.(string), string(i)) } if resp.Result.C != 2*i+1 { t.Critical("resp: bad result: %d+%d=%d", i, i+1, resp.Result.C) } } fmt.Fprintf(cli, "{}\n") var resp addResp if err := dec.Decode(&resp); err != nil { t.Critical("Decode after empty: %s", err) } if resp.Error == nil { t.Critical("Expected error, got nil") } }