network_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package rafttest
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/coreos/etcd/raft/raftpb"
  6. )
  7. func TestNetworkDrop(t *testing.T) {
  8. // drop around 10% messages
  9. sent := 1000
  10. droprate := 0.1
  11. nt := newRaftNetwork(1, 2)
  12. nt.drop(1, 2, droprate)
  13. for i := 0; i < sent; i++ {
  14. nt.send(raftpb.Message{From: 1, To: 2})
  15. }
  16. c := nt.recvFrom(2)
  17. received := 0
  18. done := false
  19. for !done {
  20. select {
  21. case <-c:
  22. received++
  23. default:
  24. done = true
  25. }
  26. }
  27. drop := sent - received
  28. if drop > int((droprate+0.1)*float64(sent)) || drop < int((droprate-0.1)*float64(sent)) {
  29. t.Errorf("drop = %d, want around %d", drop, droprate*float64(sent))
  30. }
  31. }
  32. func TestNetworkDelay(t *testing.T) {
  33. sent := 1000
  34. delay := time.Millisecond
  35. delayrate := 0.1
  36. nt := newRaftNetwork(1, 2)
  37. nt.delay(1, 2, delay, delayrate)
  38. var total time.Duration
  39. for i := 0; i < sent; i++ {
  40. s := time.Now()
  41. nt.send(raftpb.Message{From: 1, To: 2})
  42. total += time.Since(s)
  43. }
  44. w := time.Duration(float64(sent)*delayrate/2) * delay
  45. // there are pretty overhead in the send call, since it genarete random numbers.
  46. if total < w+10*delay {
  47. t.Errorf("total = %v, want > %v", total, w)
  48. }
  49. }