watch_fragment_test.go 3.7 KB

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