go.temporal.io/server@v1.23.0/common/collection/paging_iterator_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 collection 26 27 import ( 28 "errors" 29 "testing" 30 31 "github.com/stretchr/testify/suite" 32 ) 33 34 type ( 35 pagingIteratorSuite struct { 36 suite.Suite 37 } 38 ) 39 40 func TestPagingIteratorSuite(t *testing.T) { 41 s := new(pagingIteratorSuite) 42 suite.Run(t, s) 43 } 44 45 func (s *pagingIteratorSuite) SetupSuite() { 46 } 47 48 func (s *pagingIteratorSuite) TearDownSuite() { 49 50 } 51 52 func (s *pagingIteratorSuite) SetupTest() { 53 54 } 55 56 func (s *pagingIteratorSuite) TearDownTest() { 57 58 } 59 60 func (s *pagingIteratorSuite) TestIteration_NoErr() { 61 phase := 0 62 outputs := [][]int{ 63 {1, 2, 3, 4, 5}, 64 {}, 65 {6}, 66 {}, 67 } 68 tokens := [][]byte{ 69 []byte("some random token 1"), 70 []byte("some random token 2"), 71 []byte("some random token 3"), 72 []byte(nil), 73 } 74 pagingFn := func(token []byte) ([]int, []byte, error) { 75 switch phase { 76 case 0: 77 s.Equal(0, len(token)) 78 defer func() { phase++ }() 79 return outputs[phase], tokens[phase], nil 80 case 1: 81 s.Equal(tokens[0], token) 82 defer func() { phase++ }() 83 return outputs[phase], tokens[phase], nil 84 case 2: 85 s.Equal(tokens[1], token) 86 defer func() { phase++ }() 87 return outputs[phase], tokens[phase], nil 88 case 3: 89 s.Equal(tokens[2], token) 90 defer func() { phase++ }() 91 return outputs[phase], tokens[phase], nil 92 default: 93 panic("should not reach here during test") 94 } 95 } 96 97 result := []int{} 98 ite := NewPagingIterator(pagingFn) 99 for ite.HasNext() { 100 num, err := ite.Next() 101 s.Nil(err) 102 result = append(result, num) 103 } 104 s.Equal([]int{1, 2, 3, 4, 5, 6}, result) 105 } 106 107 func (s *pagingIteratorSuite) TestIteration_Err_Beginging() { 108 phase := 0 109 ite := NewPagingIterator(func(token []byte) ([]interface{}, []byte, error) { 110 switch phase { 111 case 0: 112 defer func() { phase++ }() 113 return nil, nil, errors.New("some random error") 114 default: 115 panic("should not reach here during test") 116 } 117 }) 118 119 s.True(ite.HasNext()) 120 item, err := ite.Next() 121 s.Nil(item) 122 s.NotNil(err) 123 s.False(ite.HasNext()) 124 } 125 126 func (s *pagingIteratorSuite) TestIteration_Err_NotBegining() { 127 128 phase := 0 129 outputs := [][]interface{}{ 130 {1, 2, 3, 4, 5}, 131 } 132 tokens := [][]byte{ 133 []byte("some random token 1"), 134 } 135 pagingFn := func(token []byte) ([]interface{}, []byte, error) { 136 switch phase { 137 case 0: 138 s.Equal(0, len(token)) 139 defer func() { phase++ }() 140 return outputs[phase], tokens[phase], nil 141 case 1: 142 s.Equal(tokens[0], token) 143 defer func() { phase++ }() 144 return nil, nil, errors.New("some random error") 145 default: 146 panic("should not reach here during test") 147 } 148 } 149 150 result := []int{} 151 ite := NewPagingIterator(pagingFn) 152 for ite.HasNext() { 153 item, err := ite.Next() 154 if err != nil { 155 break 156 } 157 num, ok := item.(int) 158 s.True(ok) 159 result = append(result, num) 160 } 161 s.Equal([]int{1, 2, 3, 4, 5}, result) 162 }