Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
tonybase committed Jul 24, 2023
1 parent 7229502 commit 79ea063
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
9 changes: 5 additions & 4 deletions gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "flag"
var globalRegistry = NewRegistry()

func init() {
flag.CommandLine.Var(globalRegistry, "feature-gates", "A set of key=value pairs that describe feature gates for alpha/beta/stable features.")
flag.Var(globalRegistry, "feature-gates", "A set of key=value pairs that describe feature gates for alpha/beta/stable features.")
}

// Register register a feature gate.
Expand All @@ -18,9 +18,10 @@ func MustRegister(name string, enabled bool, opts ...Option) *Feature {
return globalRegistry.MustRegister(name, enabled, opts...)
}

// Set parses the feature flags: foo=true,bar=false.
func Set(featureFlags string) error {
return globalRegistry.Set(featureFlags)
// Set sets the feature arguments.
// eg: foo=true,bar=false
func Set(args string) error {
return globalRegistry.Set(args)
}

// SetEnabled set feature enabled.
Expand Down
45 changes: 45 additions & 0 deletions gate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package feature_test

import (
"flag"
"os"
"testing"

"github.com/go-kratos/feature"
Expand All @@ -21,3 +23,46 @@ func TestFeatureVisit(t *testing.T) {
t.Logf("feature: %s %t", f.Name(), f.Enabled())
})
}

func TestFeautreTrue(t *testing.T) {
os.Args[1] = "--feature-gates=foo=true,bar=true"
flag.Parse()
if !foo.Enabled() {
t.Fatal("foo is enabled")
}
if !bar.Enabled() {
t.Fatal("bar is enabled")
}
}

func TestFeautreFalse(t *testing.T) {
os.Args[1] = "--feature-gates=foo=false,bar=false"
flag.Parse()
if foo.Enabled() {
t.Fatal("foo is enabled")
}
if bar.Enabled() {
t.Fatal("bar is enabled")
}
}

func TestFeautreRegistered(t *testing.T) {
_, err := feature.Register("foo", true)
if err == nil {
t.Fatal("foo is registered")
}
}

func TestFeautreRegister(t *testing.T) {
tf, err := feature.Register("test_false", false)
if err != nil {
t.Fatal(err)
}
if tf.Enabled() {
t.Fatal("test false")
}
feature.SetEnabled("test_false", true)
if !tf.Enabled() {
t.Fatal("test true")
}
}
27 changes: 13 additions & 14 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ func (r *Registry) Register(name string, enabled bool, opts ...Option) (*Feature
if _, ok := r.features[name]; ok {
return nil, fmt.Errorf("feature gate %s is registered", name)
}
feature := &Feature{
name: name,
}
feature := &Feature{name: name}
feature.enabled.Store(enabled)
for _, o := range opts {
o(feature)
Expand All @@ -74,19 +72,27 @@ func (r *Registry) MustRegister(name string, enabled bool, opts ...Option) *Feat
return feature
}

// Visit visits all the features.
func (r *Registry) Visit(f func(*Feature)) {
for _, feature := range r.features {
f(feature)
}
}

// SetEnabled set feature enabled.
func (r *Registry) SetEnabled(name string, enabled bool) error {
f, ok := r.features[name]
if !ok {
return fmt.Errorf("not found feature name: %s", name)
return fmt.Errorf("not found feature: %s", name)
}
f.enabled.Store(enabled)
return nil
}

// Set parses the feature flags: foo=true,bar=false.
func (r *Registry) Set(featureFlags string) error {
fs := strings.Split(featureFlags, ",")
// Set sets the feature arguments.
// eg: foo=true,bar=false
func (r *Registry) Set(args string) error {
fs := strings.Split(args, ",")
for _, s := range fs {
feature := strings.Split(s, "=")
name := feature[0]
Expand All @@ -110,10 +116,3 @@ func (r *Registry) String() string {
return strings.Join(pairs, ",")

}

// Visit visits all the features.
func (r *Registry) Visit(f func(*Feature)) {
for _, feature := range r.features {
f(feature)
}
}

0 comments on commit 79ea063

Please sign in to comment.