-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
204 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/clubpay/ronykit" | ||
) | ||
|
||
func TestSampleService(t *testing.T) { | ||
err := ronykit.NewTestContext(). | ||
SetHandler(echoHandler). | ||
Input( | ||
&echoRequest{ | ||
RandomID: 2374, | ||
Ok: false, | ||
}, | ||
nil, | ||
). | ||
Expectation( | ||
func(out ...*ronykit.Envelope) error { | ||
if len(out) != 1 { | ||
t.Fatalf("expected 1 envelope, got %d", len(out)) | ||
} | ||
out1 := out[0] | ||
res1, ok := out1.GetMsg().(*echoResponse) | ||
if !ok { | ||
t.Fatalf("expected echoResponse, got %v", reflect.TypeOf(out1.GetMsg())) | ||
} | ||
if res1.RandomID != 2374 { | ||
t.Fatalf("got unexpected randomID: %d", res1.RandomID) | ||
} | ||
|
||
return nil | ||
}, | ||
).Run() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
package common | ||
|
||
import "github.com/clubpay/ronykit" | ||
type NOPLogger struct{} | ||
|
||
type nopLogger struct{} | ||
|
||
func NewNopLogger() ronykit.Logger { | ||
return nopLogger{} | ||
func NewNopLogger() NOPLogger { | ||
return NOPLogger{} | ||
} | ||
|
||
func (n nopLogger) Debug(args ...interface{}) { | ||
func (n NOPLogger) Debug(args ...interface{}) { | ||
return | ||
} | ||
|
||
func (n nopLogger) Debugf(format string, args ...interface{}) { | ||
func (n NOPLogger) Debugf(format string, args ...interface{}) { | ||
return | ||
} | ||
|
||
func (n nopLogger) Error(args ...interface{}) { | ||
func (n NOPLogger) Error(args ...interface{}) { | ||
return | ||
} | ||
|
||
func (n nopLogger) Errorf(format string, args ...interface{}) { | ||
func (n NOPLogger) Errorf(format string, args ...interface{}) { | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package common | ||
package errors | ||
|
||
type ErrFunc func(v ...interface{}) error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package ronykit | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/clubpay/ronykit/utils" | ||
) | ||
|
||
// TestContext is useful for writing end-to-end tests for your Contract handlers. | ||
type TestContext struct { | ||
ctx *Context | ||
handlers HandlerFuncChain | ||
inMsg Message | ||
inHdr EnvelopeHdr | ||
expectFunc func(...*Envelope) error | ||
} | ||
|
||
func NewTestContext() *TestContext { | ||
return &TestContext{} | ||
} | ||
|
||
func (testCtx *TestContext) SetHandler(h ...HandlerFunc) *TestContext { | ||
testCtx.handlers = h | ||
|
||
return testCtx | ||
} | ||
|
||
func (testCtx *TestContext) Input(m Message, hdr EnvelopeHdr) *TestContext { | ||
testCtx.inMsg = m | ||
testCtx.inHdr = hdr | ||
|
||
return testCtx | ||
} | ||
|
||
func (testCtx *TestContext) Expectation(f func(out ...*Envelope) error) *TestContext { | ||
testCtx.expectFunc = f | ||
|
||
return testCtx | ||
} | ||
|
||
func (testCtx *TestContext) Run() error { | ||
ctx := newContext() | ||
conn := newTestConn() | ||
ctx.conn = conn | ||
ctx.in = newEnvelope(ctx, conn, false). | ||
SetMsg(testCtx.inMsg). | ||
SetHdrMap(testCtx.inHdr) | ||
ctx.ctx = context.Background() | ||
ctx.wf = func(conn Conn, e *Envelope) error { | ||
e.DontRelease() | ||
tc := conn.(*testConn) | ||
tc.Lock() | ||
tc.out = append(tc.out, e) | ||
tc.Unlock() | ||
|
||
return nil | ||
} | ||
ctx.handlers = append(ctx.handlers, testCtx.handlers...) | ||
ctx.Next() | ||
|
||
return testCtx.expectFunc(conn.out...) | ||
} | ||
|
||
type testConn struct { | ||
sync.Mutex | ||
|
||
id uint64 | ||
clientIP string | ||
stream bool | ||
kv map[string]string | ||
out []*Envelope | ||
} | ||
|
||
var _ Conn = (*testConn)(nil) | ||
|
||
func newTestConn() *testConn { | ||
return &testConn{ | ||
id: utils.RandomUint64(0), | ||
} | ||
} | ||
|
||
func (t *testConn) ConnID() uint64 { | ||
return t.id | ||
} | ||
|
||
func (t *testConn) ClientIP() string { | ||
return t.clientIP | ||
} | ||
|
||
func (t *testConn) Write(data []byte) (int, error) { | ||
return 0, nil | ||
} | ||
|
||
func (t *testConn) Stream() bool { | ||
return t.stream | ||
} | ||
|
||
func (t *testConn) Walk(f func(key string, val string) bool) { | ||
t.Lock() | ||
defer t.Unlock() | ||
|
||
for k, v := range t.kv { | ||
if !f(k, v) { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (t *testConn) Get(key string) string { | ||
t.Lock() | ||
defer t.Unlock() | ||
|
||
return t.kv[key] | ||
} | ||
|
||
func (t *testConn) Set(key string, val string) { | ||
t.Lock() | ||
t.kv[key] = val | ||
t.Unlock() | ||
} |