github.com/m3db/m3@v1.5.0/src/cluster/mem/mem_test.go (about)

     1  // Copyright (c) 2020 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package memcluster
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/cluster/kv"
    27  	"github.com/m3db/m3/src/cluster/placement"
    28  	"github.com/m3db/m3/src/cluster/services"
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  func TestReusesStores(t *testing.T) {
    34  	key := "my_key"
    35  
    36  	c := New(kv.NewOverrideOptions())
    37  	store, err := c.TxnStore(kv.NewOverrideOptions())
    38  	require.NoError(t, err)
    39  	version, err := store.Set(key, &dummyProtoMessage{"my_value"})
    40  	require.NoError(t, err)
    41  
    42  	// retrieve the same store
    43  	sameStore, err := c.TxnStore(kv.NewOverrideOptions())
    44  	require.NoError(t, err)
    45  
    46  	v, err := sameStore.Get(key)
    47  	require.NoError(t, err)
    48  	assert.Equal(t, version, v.Version())
    49  
    50  	// other store doesn't have the value.
    51  	otherZone, err := c.TxnStore(kv.NewOverrideOptions().SetZone("other"))
    52  	require.NoError(t, err)
    53  	_, err = otherZone.Get(key)
    54  	assert.EqualError(t, err, "key not found")
    55  }
    56  
    57  func TestServices_Placement(t *testing.T) {
    58  	c := New(kv.NewOverrideOptions())
    59  	svcs, err := c.Services(services.NewOverrideOptions())
    60  	require.NoError(t, err)
    61  
    62  	placementSvc, err := svcs.PlacementService(services.NewServiceID().SetName("test_svc"), placement.NewOptions())
    63  	require.NoError(t, err)
    64  
    65  	p := placement.NewPlacement().SetInstances([]placement.Instance{
    66  		placement.NewInstance().SetHostname("host").SetEndpoint("127.0.0.1"),
    67  	})
    68  
    69  	p, err = placementSvc.Set(p)
    70  	require.NoError(t, err)
    71  
    72  	retrieved, err := placementSvc.Placement()
    73  	require.NoError(t, err)
    74  
    75  	// n.b.: placements are hard to compare directly since they're interfaces and contain more pointers than
    76  	// they ought, and it's not worth writing the method here.
    77  	assert.Equal(t, p.Version(), retrieved.Version())
    78  }
    79  
    80  // dummyProtoMessage implements proto.Message and exists solely as a thing
    81  // to pass to a kv.Store.
    82  type dummyProtoMessage struct {
    83  	Val string
    84  }
    85  
    86  func (d *dummyProtoMessage) Reset() {
    87  }
    88  
    89  func (d *dummyProtoMessage) String() string {
    90  	return d.Val
    91  }
    92  
    93  func (d *dummyProtoMessage) ProtoMessage() {
    94  }