go.temporal.io/server@v1.23.0/common/persistence/queue_v2_util_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package persistence 26 27 import ( 28 "testing" 29 ) 30 31 func TestGetDeleteRange(t *testing.T) { 32 tests := []struct { 33 name string 34 request DeleteRequest 35 expected DeleteRange 36 shouldUpdate bool 37 }{ 38 { 39 name: "Delete within range", 40 request: DeleteRequest{ 41 LastIDToDeleteInclusive: 3, 42 ExistingMessageRange: InclusiveMessageRange{ 43 MinMessageID: 1, 44 MaxMessageID: 5, 45 }, 46 }, 47 expected: DeleteRange{ 48 InclusiveMessageRange: InclusiveMessageRange{ 49 MinMessageID: 1, 50 MaxMessageID: 3, 51 }, 52 NewMinMessageID: 4, 53 MessagesToDelete: 3, 54 }, 55 shouldUpdate: true, 56 }, 57 { 58 name: "LastID below range", 59 request: DeleteRequest{ 60 LastIDToDeleteInclusive: 0, 61 ExistingMessageRange: InclusiveMessageRange{ 62 MinMessageID: 1, 63 MaxMessageID: 5, 64 }, 65 }, 66 expected: DeleteRange{}, 67 shouldUpdate: false, 68 }, 69 { 70 name: "LastID exceeds range", 71 request: DeleteRequest{ 72 LastIDToDeleteInclusive: 6, 73 ExistingMessageRange: InclusiveMessageRange{ 74 MinMessageID: 1, 75 MaxMessageID: 5, 76 }, 77 }, 78 expected: DeleteRange{ 79 InclusiveMessageRange: InclusiveMessageRange{ 80 MinMessageID: 1, 81 MaxMessageID: 4, 82 }, 83 NewMinMessageID: 6, 84 MessagesToDelete: 5, 85 }, 86 shouldUpdate: true, 87 }, 88 { 89 name: "Delete the last but one message", 90 request: DeleteRequest{ 91 LastIDToDeleteInclusive: 4, 92 ExistingMessageRange: InclusiveMessageRange{ 93 MinMessageID: 1, 94 MaxMessageID: 5, 95 }, 96 }, 97 expected: DeleteRange{ 98 InclusiveMessageRange: InclusiveMessageRange{ 99 MinMessageID: 1, 100 MaxMessageID: 4, 101 }, 102 NewMinMessageID: 5, 103 MessagesToDelete: 4, 104 }, 105 shouldUpdate: true, 106 }, 107 { 108 name: "Single message in range", 109 request: DeleteRequest{ 110 LastIDToDeleteInclusive: 1, 111 ExistingMessageRange: InclusiveMessageRange{ 112 MinMessageID: 1, 113 MaxMessageID: 1, 114 }, 115 }, 116 expected: DeleteRange{ 117 InclusiveMessageRange: InclusiveMessageRange{ 118 MinMessageID: 1, 119 MaxMessageID: 0, 120 }, 121 NewMinMessageID: 2, 122 MessagesToDelete: 1, 123 }, 124 shouldUpdate: true, 125 }, 126 } 127 128 for _, tt := range tests { 129 t.Run(tt.name, func(t *testing.T) { 130 result, shouldDelete := GetDeleteRange(tt.request) 131 if shouldDelete != tt.shouldUpdate { 132 t.Errorf("expected %+v for shouldUpdate but got %+v", tt.shouldUpdate, shouldDelete) 133 } 134 if result != tt.expected { 135 t.Errorf("expected %+v but got %+v", tt.expected, result) 136 } 137 }) 138 } 139 }