|
| 1 | +// Copyright 2025 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 | + |
| 15 | +package txn |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + |
| 22 | + pb "go.etcd.io/etcd/api/v3/etcdserverpb" |
| 23 | +) |
| 24 | + |
| 25 | +func TestRangeLimit(t *testing.T) { |
| 26 | + tcs := []struct { |
| 27 | + name string |
| 28 | + request *pb.RangeRequest |
| 29 | + limit int64 |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "Request sort order is not NONE", |
| 33 | + request: &pb.RangeRequest{ |
| 34 | + SortOrder: pb.RangeRequest_ASCEND, |
| 35 | + Limit: 1, |
| 36 | + }, |
| 37 | + limit: 0, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Request min mod revision is not 0", |
| 41 | + request: &pb.RangeRequest{ |
| 42 | + SortOrder: pb.RangeRequest_NONE, |
| 43 | + MinModRevision: 1, |
| 44 | + Limit: 1, |
| 45 | + }, |
| 46 | + limit: 0, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "Request max mod revision is not 0", |
| 50 | + request: &pb.RangeRequest{ |
| 51 | + SortOrder: pb.RangeRequest_NONE, |
| 52 | + MinModRevision: 0, |
| 53 | + MaxModRevision: 1, |
| 54 | + Limit: 1, |
| 55 | + }, |
| 56 | + limit: 0, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Request min create revision is not 0", |
| 60 | + request: &pb.RangeRequest{ |
| 61 | + SortOrder: pb.RangeRequest_NONE, |
| 62 | + MinModRevision: 0, |
| 63 | + MaxModRevision: 0, |
| 64 | + MinCreateRevision: 1, |
| 65 | + Limit: 1, |
| 66 | + }, |
| 67 | + limit: 0, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "Request max create revision is not 0", |
| 71 | + request: &pb.RangeRequest{ |
| 72 | + SortOrder: pb.RangeRequest_NONE, |
| 73 | + MinModRevision: 0, |
| 74 | + MaxModRevision: 0, |
| 75 | + MinCreateRevision: 0, |
| 76 | + MaxCreateRevision: 1, |
| 77 | + Limit: 1, |
| 78 | + }, |
| 79 | + limit: 0, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "No extra fetch when request limit is 0", |
| 83 | + request: &pb.RangeRequest{ |
| 84 | + SortOrder: pb.RangeRequest_NONE, |
| 85 | + MinModRevision: 0, |
| 86 | + MaxModRevision: 0, |
| 87 | + MinCreateRevision: 0, |
| 88 | + MaxCreateRevision: 0, |
| 89 | + Limit: 0, |
| 90 | + }, |
| 91 | + limit: 0, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "Add one extra fetch when request limit is not 0", |
| 95 | + request: &pb.RangeRequest{ |
| 96 | + SortOrder: pb.RangeRequest_NONE, |
| 97 | + MinModRevision: 0, |
| 98 | + MaxModRevision: 0, |
| 99 | + MinCreateRevision: 0, |
| 100 | + MaxCreateRevision: 0, |
| 101 | + Limit: 1, |
| 102 | + }, |
| 103 | + limit: 2, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + for _, tt := range tcs { |
| 108 | + t.Run(tt.name, func(t *testing.T) { |
| 109 | + assert.Equal(t, tt.limit, rangeLimit(tt.request)) |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments