github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/jobmaster/dm/metadata/state_test.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package metadata
    15  
    16  import (
    17  	"bytes"
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/BurntSushi/toml"
    22  	"github.com/pingcap/tiflow/engine/pkg/meta/mock"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  type DummyState struct {
    27  	I int
    28  }
    29  
    30  func (ds *DummyState) String() string {
    31  	return "dummy state"
    32  }
    33  
    34  type DummyStore struct {
    35  	*frameworkMetaStore
    36  }
    37  
    38  func (ds *DummyStore) createState() state {
    39  	return &DummyState{}
    40  }
    41  
    42  func (ds *DummyStore) key() string {
    43  	return "dummy store"
    44  }
    45  
    46  type FailedState struct {
    47  	I int
    48  	i int
    49  }
    50  
    51  type FailedStore struct {
    52  	*frameworkMetaStore
    53  }
    54  
    55  func (fs *FailedStore) createState() state {
    56  	return &FailedState{}
    57  }
    58  
    59  func (fs *FailedStore) key() string {
    60  	return "failed store"
    61  }
    62  
    63  func TestDefaultStore(t *testing.T) {
    64  	t.Parallel()
    65  
    66  	kvClient := mock.NewMetaMock()
    67  	dummyState := &DummyState{I: 1}
    68  	dummyStore := &DummyStore{
    69  		frameworkMetaStore: newTOMLFrameworkMetaStore(kvClient),
    70  	}
    71  	dummyStore.frameworkMetaStore.stateFactory = dummyStore
    72  
    73  	state, err := dummyStore.Get(context.Background())
    74  	require.Error(t, err)
    75  	require.Nil(t, state)
    76  	require.NoError(t, dummyStore.Delete(context.Background()))
    77  
    78  	require.NoError(t, dummyStore.Put(context.Background(), dummyState))
    79  	state, err = dummyStore.Get(context.Background())
    80  	require.NoError(t, err)
    81  	require.Equal(t, dummyState, state)
    82  
    83  	dummyState = &DummyState{I: 2}
    84  	require.NoError(t, dummyStore.Put(context.Background(), dummyState))
    85  	state, err = dummyStore.Get(context.Background())
    86  	require.NoError(t, err)
    87  	require.Equal(t, dummyState, state)
    88  
    89  	require.NoError(t, dummyStore.Delete(context.Background()))
    90  	state, err = dummyStore.Get(context.Background())
    91  	require.Error(t, err)
    92  	require.Nil(t, state)
    93  
    94  	var b bytes.Buffer
    95  	err = toml.NewEncoder(&b).Encode(dummyState)
    96  	require.NoError(t, err)
    97  	kvClient.Put(context.Background(), dummyStore.key(), b.String())
    98  	state, err = dummyStore.Get(context.Background())
    99  	require.NoError(t, err)
   100  	require.Equal(t, dummyState, state)
   101  	state, err = dummyStore.Get(context.Background())
   102  	require.NoError(t, err)
   103  	require.Equal(t, dummyState, state)
   104  
   105  	dummyState = state.(*DummyState)
   106  	require.Equal(t, dummyState.String(), "dummy state")
   107  
   108  	failedState := &FailedState{I: 1, i: 2}
   109  	failedStore := &FailedStore{
   110  		frameworkMetaStore: newTOMLFrameworkMetaStore(kvClient),
   111  	}
   112  	failedStore.frameworkMetaStore.stateFactory = failedStore
   113  	require.EqualError(t, failedStore.Put(context.Background(), failedState), "fields of state should all be public")
   114  }