watch_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2018 The etcd Authors
  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 v3rpc
  15. import (
  16. "bytes"
  17. "math"
  18. "testing"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/mvcc/mvccpb"
  21. )
  22. func TestSendFragment(t *testing.T) {
  23. tt := []struct {
  24. wr *pb.WatchResponse
  25. maxRequestBytes int
  26. fragments int
  27. werr error
  28. }{
  29. { // large limit should not fragment
  30. wr: createResponse(100, 1),
  31. maxRequestBytes: math.MaxInt32,
  32. fragments: 1,
  33. },
  34. { // large limit for two messages, expect no fragment
  35. wr: createResponse(10, 2),
  36. maxRequestBytes: 50,
  37. fragments: 1,
  38. },
  39. { // limit is small but only one message, expect no fragment
  40. wr: createResponse(1024, 1),
  41. maxRequestBytes: 1,
  42. fragments: 1,
  43. },
  44. { // exceed limit only when combined, expect fragments
  45. wr: createResponse(11, 5),
  46. maxRequestBytes: 20,
  47. fragments: 5,
  48. },
  49. { // 5 events with each event exceeding limits, expect fragments
  50. wr: createResponse(15, 5),
  51. maxRequestBytes: 10,
  52. fragments: 5,
  53. },
  54. { // 4 events with some combined events exceeding limits
  55. wr: createResponse(10, 4),
  56. maxRequestBytes: 35,
  57. fragments: 2,
  58. },
  59. }
  60. for i := range tt {
  61. fragmentedResp := make([]*pb.WatchResponse, 0)
  62. testSend := func(wr *pb.WatchResponse) error {
  63. fragmentedResp = append(fragmentedResp, wr)
  64. return nil
  65. }
  66. err := sendFragments(tt[i].wr, tt[i].maxRequestBytes, testSend)
  67. if err != tt[i].werr {
  68. t.Errorf("#%d: expected error %v, got %v", i, tt[i].werr, err)
  69. }
  70. got := len(fragmentedResp)
  71. if got != tt[i].fragments {
  72. t.Errorf("#%d: expected response number %d, got %d", i, tt[i].fragments, got)
  73. }
  74. if got > 0 && fragmentedResp[got-1].Fragment {
  75. t.Errorf("#%d: expected fragment=false in last response, got %+v", i, fragmentedResp[got-1])
  76. }
  77. }
  78. }
  79. func createResponse(dataSize, events int) (resp *pb.WatchResponse) {
  80. resp = &pb.WatchResponse{Events: make([]*mvccpb.Event, events)}
  81. for i := range resp.Events {
  82. resp.Events[i] = &mvccpb.Event{
  83. Kv: &mvccpb.KeyValue{
  84. Key: bytes.Repeat([]byte("a"), dataSize),
  85. },
  86. }
  87. }
  88. return resp
  89. }