mirror of
https://github.com/cubefs/cubefs.git
synced 2026-08-02 02:00:56 +00:00
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
// Copyright 2022 The CubeFS Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
// implied. See the License for the specific language governing
|
|
// permissions and limitations under the License.
|
|
|
|
package taskswitch
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
errcode "github.com/cubefs/cubefs/blobstore/common/errors"
|
|
)
|
|
|
|
func TestTaskSwitch(t *testing.T) {
|
|
ts := newTaskSwitch()
|
|
require.Equal(t, false, ts.Enabled())
|
|
ts.Enable()
|
|
require.Equal(t, true, ts.Enabled())
|
|
ts.Disable()
|
|
require.Equal(t, false, ts.Enabled())
|
|
}
|
|
|
|
type mockCfgGetter struct {
|
|
m map[string]string
|
|
}
|
|
|
|
func (cfgGetter *mockCfgGetter) GetConfig(ctx context.Context, key string) (val string, err error) {
|
|
if val, ok := cfgGetter.m[key]; ok {
|
|
return val, nil
|
|
}
|
|
return "", errcode.ErrNotFound
|
|
}
|
|
|
|
func (cfgGetter *mockCfgGetter) SetConfig(ctx context.Context, key, value string) (err error) {
|
|
cfgGetter.m[key] = value
|
|
return nil
|
|
}
|
|
|
|
func TestSwitchMgr(t *testing.T) {
|
|
cfgGetter := mockCfgGetter{
|
|
m: make(map[string]string),
|
|
}
|
|
cfgGetter.m["switch1"] = SwitchOpen
|
|
sm := SwitchMgr{accessor: &cfgGetter, switchs: make(map[string]*TaskSwitch)}
|
|
s1, err := sm.AddSwitch("switch1")
|
|
require.NoError(t, err)
|
|
s2, err := sm.AddSwitch("switch2")
|
|
require.NoError(t, err)
|
|
|
|
sm.update()
|
|
require.Equal(t, true, s1.Enabled())
|
|
require.Equal(t, false, s2.Enabled())
|
|
|
|
sm.update()
|
|
err = sm.DelSwitch("switch1")
|
|
require.NoError(t, err)
|
|
err = sm.DelSwitch("switch2")
|
|
require.NoError(t, err)
|
|
require.Equal(t, 0, len(sm.switchs))
|
|
}
|