go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/impl/memory/memstore_test.go (about)

     1  // Copyright 2015 The LUCI 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 memory
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  type keyLeftRight struct{ key, left, right []byte }
    24  
    25  type kv struct{ k, v []byte }
    26  
    27  var testCollisionCases = []struct {
    28  	name        string
    29  	left, right []kv // inserts into left and right collections
    30  	expect      []keyLeftRight
    31  }{
    32  	{
    33  		name: "nil",
    34  	},
    35  	{
    36  		name:  "empty",
    37  		left:  []kv{},
    38  		right: []kv{},
    39  	},
    40  	{
    41  		name: "all old",
    42  		left: []kv{
    43  			{cat(1), cat()},
    44  			{cat(0), cat()},
    45  		},
    46  		expect: []keyLeftRight{
    47  			{cat(0), cat(), nil},
    48  			{cat(1), cat(), nil},
    49  		},
    50  	},
    51  	{
    52  		name: "all new",
    53  		right: []kv{
    54  			{cat(1), cat()},
    55  			{cat(0), cat()},
    56  		},
    57  		expect: []keyLeftRight{
    58  			{cat(0), nil, cat()},
    59  			{cat(1), nil, cat()},
    60  		},
    61  	},
    62  	{
    63  		name: "new vals",
    64  		left: []kv{
    65  			{cat(1), cat("hi")},
    66  			{cat(0), cat("newb")},
    67  		},
    68  		right: []kv{
    69  			{cat(0), cat(2.5)},
    70  			{cat(1), cat(58)},
    71  		},
    72  		expect: []keyLeftRight{
    73  			{cat(0), cat("newb"), cat(2.5)},
    74  			{cat(1), cat("hi"), cat(58)},
    75  		},
    76  	},
    77  	{
    78  		name: "mixed",
    79  		left: []kv{
    80  			{cat(1), cat("one")},
    81  			{cat(0), cat("hi")},
    82  			{cat(6), cat()},
    83  			{cat(3), cat(1.3)},
    84  			{cat(2), []byte("zoop")},
    85  			{cat(-1), cat("bob")},
    86  		},
    87  		right: []kv{
    88  			{cat(3), cat(1)},
    89  			{cat(1), cat(58)},
    90  			{cat(0), cat(2.5)},
    91  			{cat(4), cat(1337)},
    92  			{cat(2), cat("ski", 7)},
    93  			{cat(20), cat("nerd")},
    94  		},
    95  		expect: []keyLeftRight{
    96  			{cat(-1), cat("bob"), nil},
    97  			{cat(0), cat("hi"), cat(2.5)},
    98  			{cat(1), cat("one"), cat(58)},
    99  			{cat(2), []byte("zoop"), cat("ski", 7)},
   100  			{cat(3), cat(1.3), cat(1)},
   101  			{cat(4), nil, cat(1337)},
   102  			{cat(6), cat(), nil},
   103  			{cat(20), nil, cat("nerd")},
   104  		},
   105  	},
   106  }
   107  
   108  func getFilledColl(fill []kv) memCollection {
   109  	if fill == nil {
   110  		return nil
   111  	}
   112  	store := newMemStore()
   113  	ret := store.GetOrCreateCollection("")
   114  	for _, i := range fill {
   115  		ret.Set(i.k, i.v)
   116  	}
   117  	return store.Snapshot().GetCollection("")
   118  }
   119  
   120  func TestCollision(t *testing.T) {
   121  	t.Parallel()
   122  
   123  	Convey("Test memStoreCollide", t, func() {
   124  		for _, tc := range testCollisionCases {
   125  			Convey(tc.name, func() {
   126  				left := getFilledColl(tc.left)
   127  				right := getFilledColl(tc.right)
   128  				i := 0
   129  				memStoreCollide(left, right, func(key, left, right []byte) {
   130  					e := tc.expect[i]
   131  					So(key, ShouldResemble, e.key)
   132  					So(left, ShouldResemble, e.left)
   133  					So(right, ShouldResemble, e.right)
   134  					i++
   135  				})
   136  				So(i, ShouldEqual, len(tc.expect))
   137  			})
   138  		}
   139  	})
   140  }