watch_fragment_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 integration
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/integration"
  23. "github.com/coreos/etcd/pkg/testutil"
  24. )
  25. // TestWatchFragmentDisable ensures that large watch
  26. // response exceeding server-side request limit can
  27. // arrive even without watch response fragmentation.
  28. func TestWatchFragmentDisable(t *testing.T) {
  29. testWatchFragment(t, false, false)
  30. }
  31. // TestWatchFragmentDisableWithGRPCLimit verifies
  32. // large watch response exceeding server-side request
  33. // limit and client-side gRPC response receive limit
  34. // cannot arrive without watch events fragmentation,
  35. // because multiple events exceed client-side gRPC
  36. // response receive limit.
  37. func TestWatchFragmentDisableWithGRPCLimit(t *testing.T) {
  38. testWatchFragment(t, false, true)
  39. }
  40. // TestWatchFragmentEnable ensures that large watch
  41. // response exceeding server-side request limit arrive
  42. // with watch response fragmentation.
  43. func TestWatchFragmentEnable(t *testing.T) {
  44. testWatchFragment(t, true, false)
  45. }
  46. // TestWatchFragmentEnableWithGRPCLimit verifies
  47. // large watch response exceeding server-side request
  48. // limit and client-side gRPC response receive limit
  49. // can arrive only when watch events are fragmented.
  50. func TestWatchFragmentEnableWithGRPCLimit(t *testing.T) {
  51. testWatchFragment(t, true, true)
  52. }
  53. // testWatchFragment triggers watch response that spans over multiple
  54. // revisions exceeding server request limits when combined.
  55. func testWatchFragment(t *testing.T, fragment, exceedRecvLimit bool) {
  56. cfg := &integration.ClusterConfig{
  57. Size: 1,
  58. MaxRequestBytes: 1.5 * 1024 * 1024,
  59. }
  60. if exceedRecvLimit {
  61. cfg.ClientMaxCallRecvMsgSize = 1.5 * 1024 * 1024
  62. }
  63. clus := integration.NewClusterV3(t, cfg)
  64. defer clus.Terminate(t)
  65. cli := clus.Client(0)
  66. errc := make(chan error)
  67. for i := 0; i < 10; i++ {
  68. go func(i int) {
  69. _, err := cli.Put(context.TODO(),
  70. fmt.Sprint("foo", i),
  71. strings.Repeat("a", 1024*1024),
  72. )
  73. errc <- err
  74. }(i)
  75. }
  76. for i := 0; i < 10; i++ {
  77. if err := <-errc; err != nil {
  78. t.Fatalf("failed to put: %v", err)
  79. }
  80. }
  81. opts := []clientv3.OpOption{clientv3.WithPrefix(), clientv3.WithRev(1)}
  82. if fragment {
  83. opts = append(opts, clientv3.WithFragment())
  84. }
  85. wch := cli.Watch(context.TODO(), "foo", opts...)
  86. // expect 10 MiB watch response
  87. select {
  88. case ws := <-wch:
  89. // without fragment, should exceed gRPC client receive limit
  90. if !fragment && exceedRecvLimit {
  91. if len(ws.Events) != 0 {
  92. t.Fatalf("expected 0 events with watch fragmentation, got %d", len(ws.Events))
  93. }
  94. exp := "code = ResourceExhausted desc = grpc: received message larger than max ("
  95. if !strings.Contains(ws.Err().Error(), exp) {
  96. t.Fatalf("expected 'ResourceExhausted' error, got %v", ws.Err())
  97. }
  98. return
  99. }
  100. // still expect merged watch events
  101. if len(ws.Events) != 10 {
  102. t.Fatalf("expected 10 events with watch fragmentation, got %d", len(ws.Events))
  103. }
  104. if ws.Err() != nil {
  105. t.Fatalf("unexpected error %v", ws.Err())
  106. }
  107. case <-time.After(testutil.RequestTimeout):
  108. t.Fatalf("took too long to receive events")
  109. }
  110. }