From 8b1a579c9d6cc54b9746f92477a06e49f9ebd97b Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 17 Apr 2022 16:25:42 +0300 Subject: [PATCH] add context tests Signed-off-by: Vasiliy Tolstov --- broker/context_test.go | 57 ++++++++++++++++++ client/context_test.go | 57 ++++++++++++++++++ codec/context_test.go | 35 +++++++++++ config/context_test.go | 73 +++++++++++++++++++++++ context_test.go | 24 ++++++++ flow/context_test.go | 35 +++++++++++ logger/context_test.go | 35 +++++++++++ metadata/context_test.go | 125 +++++++++++++++++++++++++++++++++++++++ meter/context_test.go | 36 +++++++++++ register/context_test.go | 36 +++++++++++ router/context_test.go | 36 +++++++++++ server/context_test.go | 46 ++++++++++++++ server/noop.go | 2 +- server/noop_test.go | 2 + store/context_test.go | 36 +++++++++++ tracer/context_test.go | 25 ++++++++ 16 files changed, 659 insertions(+), 1 deletion(-) create mode 100644 broker/context_test.go create mode 100644 client/context_test.go create mode 100644 codec/context_test.go create mode 100644 config/context_test.go create mode 100644 context_test.go create mode 100644 flow/context_test.go create mode 100644 logger/context_test.go create mode 100644 metadata/context_test.go create mode 100644 meter/context_test.go create mode 100644 register/context_test.go create mode 100644 router/context_test.go create mode 100644 server/context_test.go create mode 100644 store/context_test.go create mode 100644 tracer/context_test.go diff --git a/broker/context_test.go b/broker/context_test.go new file mode 100644 index 00000000..65337cfe --- /dev/null +++ b/broker/context_test.go @@ -0,0 +1,57 @@ +package broker + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), brokerKey{}, NewBroker()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewBroker()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} + +func TestSetSubscribeOption(t *testing.T) { + type key struct{} + o := SetSubscribeOption(key{}, "test") + opts := &SubscribeOptions{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetSubscribeOption not works") + } +} + +func TestSetPublishOption(t *testing.T) { + type key struct{} + o := SetPublishOption(key{}, "test") + opts := &PublishOptions{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetPublishOption not works") + } +} + +func TestSetOption(t *testing.T) { + type key struct{} + o := SetOption(key{}, "test") + opts := &Options{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetOption not works") + } +} diff --git a/client/context_test.go b/client/context_test.go new file mode 100644 index 00000000..3f29ace5 --- /dev/null +++ b/client/context_test.go @@ -0,0 +1,57 @@ +package client + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), clientKey{}, NewClient()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewClient()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} + +func TestSetPublishOption(t *testing.T) { + type key struct{} + o := SetPublishOption(key{}, "test") + opts := &PublishOptions{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetPublishOption not works") + } +} + +func TestSetCallOption(t *testing.T) { + type key struct{} + o := SetCallOption(key{}, "test") + opts := &CallOptions{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetCallOption not works") + } +} + +func TestSetOption(t *testing.T) { + type key struct{} + o := SetOption(key{}, "test") + opts := &Options{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetOption not works") + } +} diff --git a/codec/context_test.go b/codec/context_test.go new file mode 100644 index 00000000..1e9e7c5c --- /dev/null +++ b/codec/context_test.go @@ -0,0 +1,35 @@ +package codec + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), codecKey{}, NewCodec()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewCodec()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} + +func TestSetOption(t *testing.T) { + type key struct{} + o := SetOption(key{}, "test") + opts := &Options{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetOption not works") + } +} diff --git a/config/context_test.go b/config/context_test.go new file mode 100644 index 00000000..3d11c5aa --- /dev/null +++ b/config/context_test.go @@ -0,0 +1,73 @@ +package config + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), configKey{}, NewConfig()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewConfig()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} + +func TestSetOption(t *testing.T) { + type key struct{} + o := SetOption(key{}, "test") + opts := &Options{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetOption not works") + } +} + + +func TestSetSaveOption(t *testing.T) { + type key struct{} + o := SetSaveOption(key{}, "test") + opts := &SaveOptions{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetSaveOption not works") + } +} + + + +func TestSetLoadOption(t *testing.T) { + type key struct{} + o := SetLoadOption(key{}, "test") + opts := &LoadOptions{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetLoadOption not works") + } +} + + + +func TestSetWatchOption(t *testing.T) { + type key struct{} + o := SetWatchOption(key{}, "test") + opts := &WatchOptions{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetWatchOption not works") + } +} diff --git a/context_test.go b/context_test.go new file mode 100644 index 00000000..2329568d --- /dev/null +++ b/context_test.go @@ -0,0 +1,24 @@ +package micro + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), serviceKey{}, NewService()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewService()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} diff --git a/flow/context_test.go b/flow/context_test.go new file mode 100644 index 00000000..b767789f --- /dev/null +++ b/flow/context_test.go @@ -0,0 +1,35 @@ +package flow + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), flowKey{}, NewFlow()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewFlow()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} + +func TestSetOption(t *testing.T) { + type key struct{} + o := SetOption(key{}, "test") + opts := &Options{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetOption not works") + } +} diff --git a/logger/context_test.go b/logger/context_test.go new file mode 100644 index 00000000..20ed4131 --- /dev/null +++ b/logger/context_test.go @@ -0,0 +1,35 @@ +package logger + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), loggerKey{}, NewLogger()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewLogger()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} + +func TestSetOption(t *testing.T) { + type key struct{} + o := SetOption(key{}, "test") + opts := &Options{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetOption not works") + } +} diff --git a/metadata/context_test.go b/metadata/context_test.go new file mode 100644 index 00000000..a07dfd13 --- /dev/null +++ b/metadata/context_test.go @@ -0,0 +1,125 @@ +package metadata + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), mdKey{}, &rawMetadata{New(0)}) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), New(0)) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} + +func TestFromIncomingContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), mdIncomingKey{}, &rawMetadata{New(0)}) + + c, ok := FromIncomingContext(ctx) + if c == nil || !ok { + t.Fatal("FromIncomingContext not works") + } +} + +func TestFromOutgoingContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), mdOutgoingKey{}, &rawMetadata{New(0)}) + + c, ok := FromOutgoingContext(ctx) + if c == nil || !ok { + t.Fatal("FromOutgoingContext not works") + } +} + + +func TestSetIncomingContext(t *testing.T) { + md := New(1) + md.Set("key", "val") + ctx := context.WithValue(context.TODO(), mdIncomingKey{}, &rawMetadata{}) + if !SetIncomingContext(ctx, md) { + t.Fatal("SetIncomingContext not works") + } + md, ok := FromIncomingContext(ctx) + if md == nil || !ok { + t.Fatal("SetIncomingContext not works") + } else if v, ok := md.Get("key"); !ok || v != "val" { + t.Fatal("SetIncomingContext not works") + } +} + +func TestSetOutgoingContext(t *testing.T) { + md := New(1) + md.Set("key", "val") + ctx := context.WithValue(context.TODO(), mdOutgoingKey{}, &rawMetadata{}) + if !SetOutgoingContext(ctx, md) { + t.Fatal("SetOutgoingContext not works") + } + md, ok := FromOutgoingContext(ctx) + if md == nil || !ok { + t.Fatal("SetOutgoingContext not works") + } else if v, ok := md.Get("key"); !ok || v != "val" { + t.Fatal("SetOutgoingContext not works") + } +} + + +func TestNewIncomingContext(t *testing.T) { + md := New(1) + md.Set("key", "val") + ctx := NewIncomingContext(context.TODO(), md) + + c, ok := FromIncomingContext(ctx) + if c == nil || !ok { + t.Fatal("NewIncomingContext not works") + } +} + +func TestNewOutgoingContext(t *testing.T) { + md := New(1) + md.Set("key", "val") + ctx := NewOutgoingContext(context.TODO(), md) + + c, ok := FromOutgoingContext(ctx) + if c == nil || !ok { + t.Fatal("NewOutgoingContext not works") + } +} + + +func TestAppendIncomingContext(t *testing.T) { + md := New(1) + md.Set("key1", "val1") + ctx := AppendIncomingContext(context.TODO(), "key2","val2") + + nmd, ok := FromIncomingContext(ctx) + if nmd == nil || !ok { + t.Fatal("AppendIncomingContext not works") + } + if v, ok := nmd.Get("key2"); !ok || v != "val2"{ + t.Fatal("AppendIncomingContext not works") + } +} + +func TestAppendOutgoingContext(t *testing.T) { + md := New(1) + md.Set("key1", "val1") + ctx := AppendOutgoingContext(context.TODO(), "key2","val2") + + nmd, ok := FromOutgoingContext(ctx) + if nmd == nil || !ok { + t.Fatal("AppendOutgoingContext not works") + } + if v, ok := nmd.Get("key2"); !ok || v != "val2"{ + t.Fatal("AppendOutgoingContext not works") + } +} \ No newline at end of file diff --git a/meter/context_test.go b/meter/context_test.go new file mode 100644 index 00000000..3bca807b --- /dev/null +++ b/meter/context_test.go @@ -0,0 +1,36 @@ +package meter + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), meterKey{}, NewMeter()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewMeter()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} + +func TestSetOption(t *testing.T) { + type key struct{} + o := SetOption(key{}, "test") + opts := &Options{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetOption not works") + } +} + diff --git a/register/context_test.go b/register/context_test.go new file mode 100644 index 00000000..d35ad88d --- /dev/null +++ b/register/context_test.go @@ -0,0 +1,36 @@ +package register + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), registerKey{}, NewRegister()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewRegister()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} + +func TestSetOption(t *testing.T) { + type key struct{} + o := SetOption(key{}, "test") + opts := &Options{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetOption not works") + } +} + diff --git a/router/context_test.go b/router/context_test.go new file mode 100644 index 00000000..286a775b --- /dev/null +++ b/router/context_test.go @@ -0,0 +1,36 @@ +package router + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), routerKey{}, NewRouter()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewRouter()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} + +func TestSetOption(t *testing.T) { + type key struct{} + o := SetOption(key{}, "test") + opts := &Options{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetOption not works") + } +} + diff --git a/server/context_test.go b/server/context_test.go new file mode 100644 index 00000000..7afbdb3f --- /dev/null +++ b/server/context_test.go @@ -0,0 +1,46 @@ +package server + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), serverKey{}, NewServer()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewServer()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} + +func TestSetOption(t *testing.T) { + type key struct{} + o := SetOption(key{}, "test") + opts := &Options{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetOption not works") + } +} + +func TestSetSubscriberOption(t *testing.T) { + type key struct{} + o := SetSubscriberOption(key{}, "test") + opts := &SubscriberOptions{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetSubscriberOption not works") + } +} diff --git a/server/noop.go b/server/noop.go index a89c03d1..8334cd2e 100644 --- a/server/noop.go +++ b/server/noop.go @@ -281,7 +281,7 @@ func (n *noopServer) Deregister() error { if sb.Options().Context != nil { cx = sb.Options().Context } - + ncx := cx wg.Add(1) go func(s broker.Subscriber) { diff --git a/server/noop_test.go b/server/noop_test.go index b343e08d..bb9e74b5 100644 --- a/server/noop_test.go +++ b/server/noop_test.go @@ -10,6 +10,7 @@ import ( "go.unistack.org/micro/v3/codec" "go.unistack.org/micro/v3/metadata" "go.unistack.org/micro/v3/server" + "go.unistack.org/micro/v3/logger" ) type TestHandler struct { @@ -50,6 +51,7 @@ func TestNoopSub(t *testing.T) { t.Fatal(err) } + logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel)) s := server.NewServer( server.Broker(b), server.Codec("application/octet-stream", codec.NewCodec()), diff --git a/store/context_test.go b/store/context_test.go new file mode 100644 index 00000000..6e081906 --- /dev/null +++ b/store/context_test.go @@ -0,0 +1,36 @@ +package store + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(), storeKey{}, NewStore()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewStore()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} + +func TestSetOption(t *testing.T) { + type key struct{} + o := SetOption(key{}, "test") + opts := &Options{} + o(opts) + + if v, ok := opts.Context.Value(key{}).(string); !ok || v == "" { + t.Fatal("SetOption not works") + } +} + diff --git a/tracer/context_test.go b/tracer/context_test.go new file mode 100644 index 00000000..bbbf3440 --- /dev/null +++ b/tracer/context_test.go @@ -0,0 +1,25 @@ +package tracer + +import ( + "context" + "testing" +) + +func TestFromContext(t *testing.T) { + ctx := context.WithValue(context.TODO(),tracerKey{}, NewTracer()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("FromContext not works") + } +} + +func TestNewContext(t *testing.T) { + ctx := NewContext(context.TODO(), NewTracer()) + + c, ok := FromContext(ctx) + if c == nil || !ok { + t.Fatal("NewContext not works") + } +} +