You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

112 line
2.6 KiB

  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package marbl
  15. import (
  16. "fmt"
  17. "math/rand"
  18. "net"
  19. "net/http"
  20. "strconv"
  21. "testing"
  22. "time"
  23. "golang.org/x/net/websocket"
  24. )
  25. func TestStreamsInSentOrder(t *testing.T) {
  26. //t.Skip("skipping test to deflake.")
  27. l, err := net.Listen("tcp", "localhost:0")
  28. if err != nil {
  29. t.Fatalf("net.Listen(): got %v, want no error", err)
  30. }
  31. handler := NewHandler()
  32. go http.Serve(l, handler)
  33. ws, err := websocket.Dial(fmt.Sprintf("ws://%s", l.Addr()), "", "http://localhost/")
  34. if err != nil {
  35. t.Fatalf("websocket.Dial(): got %v, want no error", err)
  36. }
  37. var iterations int64 = 5000
  38. go func() {
  39. for i := int64(0); i < iterations; i++ {
  40. hex := strconv.FormatInt(int64(i), 16)
  41. handler.Write([]byte(hex))
  42. }
  43. }()
  44. for i := int64(0); i < iterations; i++ {
  45. var bytes []byte
  46. err = websocket.Message.Receive(ws, &bytes)
  47. if err != nil {
  48. t.Fatalf("websocket.Conn.Read(): got %v, want no error", err)
  49. }
  50. parsed, err := strconv.ParseInt(string(bytes), 16, 64)
  51. if err != nil {
  52. t.Fatalf("strconv.ParseInt(): got %v, want no error", err)
  53. }
  54. if parsed != i {
  55. t.Errorf("Messages arrived out of order, expected %d got %d", i, parsed)
  56. }
  57. }
  58. }
  59. func TestUnreadsDontBlock(t *testing.T) {
  60. l, err := net.Listen("tcp", "localhost:0")
  61. if err != nil {
  62. t.Fatalf("net.Listen(): got %v, want no error", err)
  63. }
  64. handler := NewHandler()
  65. go http.Serve(l, handler)
  66. _, err = websocket.Dial(fmt.Sprintf("ws://%s", l.Addr()), "", "http://localhost/")
  67. if err != nil {
  68. t.Fatalf("websocket.Dial(): got %v, want no error", err)
  69. }
  70. bytes := make([]byte, 1024)
  71. _, err = rand.Read(bytes)
  72. if err != nil {
  73. t.Fatalf("rand.Read(): got %v, want no error", err)
  74. }
  75. var iterations int64 = 50000
  76. for i := int64(0); i < iterations; i++ {
  77. to := doOrTimeout(3*time.Second, func() {
  78. handler.Write(bytes)
  79. })
  80. if to {
  81. t.Fatalf("handler.Write() Timed out")
  82. }
  83. }
  84. }
  85. func doOrTimeout(d time.Duration, f func()) bool {
  86. done := make(chan interface{})
  87. go func() {
  88. f()
  89. done <- 1
  90. }()
  91. select {
  92. case <-done:
  93. return false
  94. case <-time.After(d):
  95. return true
  96. }
  97. }