Skip to content

Commit

Permalink
Use dummy processor for testing
Browse files Browse the repository at this point in the history
Summary:
Remove manual/hardcoded Processor implementations in favor of the new codegen'd Dummy processor.

This simplifies things a lot and makes it easy to extend the dummy schema in the future.

Reviewed By: leoleovich

Differential Revision: D65962913

fbshipit-source-id: 915907d943b937b07bdb05a1b07853f43c71c2e9
  • Loading branch information
echistyakov authored and facebook-github-bot committed Nov 15, 2024
1 parent b40878e commit 7f33f57
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 235 deletions.
4 changes: 3 additions & 1 deletion thrift/lib/go/thrift/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"testing"
"time"

"github.com/facebook/fbthrift/thrift/lib/go/thrift/dummy"
"github.com/facebook/fbthrift/thrift/lib/go/thrift/types"
)

Expand All @@ -34,7 +35,8 @@ func TestClient(t *testing.T) {
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
server := NewServer(&testProcessor{}, listener, TransportIDRocket)
processor := dummy.NewDummyProcessor(&dummy.DummyHandler{})
server := NewServer(processor, listener, TransportIDRocket)
serverCtx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
Expand Down
44 changes: 9 additions & 35 deletions thrift/lib/go/thrift/header_simple_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,9 @@ import (
"testing"
"time"

"github.com/facebook/fbthrift/thrift/lib/go/thrift/types"
"github.com/facebook/fbthrift/thrift/lib/thrift/metadata"
"github.com/facebook/fbthrift/thrift/lib/go/thrift/dummy"
)

type headerServerTestProcessor struct {
requests chan<- *MyTestStruct
}

func (t *headerServerTestProcessor) ProcessorFunctionMap() map[string]types.ProcessorFunction {
return map[string]types.ProcessorFunction{"test": &headerServerTestProcessorFunction{&testProcessorFunction{}, t.requests}}
}

func (t *headerServerTestProcessor) GetThriftMetadata() *metadata.ThriftMetadata {
return nil
}

type headerServerTestProcessorFunction struct {
types.ProcessorFunction
requests chan<- *MyTestStruct
}

func (p *headerServerTestProcessorFunction) RunContext(ctx context.Context, reqStruct types.Struct) (types.WritableStruct, types.ApplicationException) {
if p.requests != nil {
p.requests <- reqStruct.(*MyTestStruct)
}
return reqStruct, nil
}

// Test that header server stops serving if listener is closed.
func TestHeaderServerCloseListener(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -60,7 +35,8 @@ func TestHeaderServerCloseListener(t *testing.T) {
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
server := NewSimpleServer(&headerServerTestProcessor{}, listener, TransportIDHeader)
processor := dummy.NewDummyProcessor(&dummy.DummyHandler{})
server := NewSimpleServer(processor, listener, TransportIDHeader)
go func() {
errChan <- server.ServeContext(ctx)
}()
Expand All @@ -73,16 +49,14 @@ func TestHeaderServerCloseListener(t *testing.T) {
if err != nil {
t.Fatalf("could not create client protocol: %s", err)
}
client := NewSerialChannel(proto)
req := &MyTestStruct{
St: "hello",
}
resp := &MyTestStruct{}
if err := client.Call(context.Background(), "test", req, resp); err != nil {
client := dummy.NewDummyChannelClient(NewSerialChannel(proto))
defer client.Close()
result, err := client.Echo(context.TODO(), "hello")
if err != nil {
t.Fatalf("could not complete call: %v", err)
}
if resp.St != "hello" {
t.Fatalf("expected response to be a hello, got %s", resp.St)
if result != "hello" {
t.Fatalf("expected response to be a hello, got %s", result)
}
listener.Close()
select {
Expand Down
41 changes: 16 additions & 25 deletions thrift/lib/go/thrift/rocket_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package thrift

import (
"bytes"
"context"
"net"
"testing"
"time"

"github.com/facebook/fbthrift/thrift/lib/go/thrift/dummy"
"github.com/facebook/fbthrift/thrift/lib/go/thrift/types"
)

Expand All @@ -44,7 +44,8 @@ func TestRocketClientClose(t *testing.T) {
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
server := NewSimpleServer(&rocketServerTestProcessor{}, listener, TransportIDRocket)
processor := dummy.NewDummyProcessor(&dummy.DummyHandler{})
server := NewSimpleServer(processor, listener, TransportIDRocket)
go func() {
errChan <- server.ServeContext(ctx)
}()
Expand All @@ -58,19 +59,13 @@ func TestRocketClientClose(t *testing.T) {
if err != nil {
t.Fatalf("could not create client protocol: %s", err)
}
client := NewSerialChannel(proto)
req := &MyTestStruct{
St: "hello",
}
resp := &MyTestStruct{}
if err := client.Call(context.Background(), "test", req, resp); err != nil {
client := dummy.NewDummyChannelClient(NewSerialChannel(proto))
result, err := client.Echo(context.TODO(), "hello")
if err != nil {
t.Fatalf("could not complete call: %v", err)
}
if resp.St != "hello" {
t.Fatalf("expected response to be a hello, got %s", resp.St)
}
if !bytes.Equal(resp.GetBin(), []byte(conn.LocalAddr().String())) {
t.Fatalf("expected response to be an address %s, got %s", conn.LocalAddr().String(), resp.GetBin())
if result != "hello" {
t.Fatalf("expected response to be a hello, got %s", result)
}
go client.Close()
select {
Expand All @@ -90,7 +85,8 @@ func TestRocketClientUnix(t *testing.T) {
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
server := NewSimpleServer(&rocketServerTestProcessor{}, listener, TransportIDRocket)
processor := dummy.NewDummyProcessor(&dummy.DummyHandler{})
server := NewSimpleServer(processor, listener, TransportIDRocket)
go func() {
errChan <- server.ServeContext(ctx)
}()
Expand All @@ -103,19 +99,14 @@ func TestRocketClientUnix(t *testing.T) {
if err != nil {
t.Fatalf("could not create client protocol: %s", err)
}
client := NewSerialChannel(proto)
req := &MyTestStruct{
St: "hello",
}
resp := &MyTestStruct{}
if err := client.Call(context.Background(), "test", req, resp); err != nil {
client := dummy.NewDummyChannelClient(NewSerialChannel(proto))
defer client.Close()
result, err := client.Echo(context.TODO(), "hello")
if err != nil {
t.Fatalf("could not complete call: %v", err)
}
if resp.St != "hello" {
t.Fatalf("expected response to be a hello, got %s", resp.St)
}
if !bytes.Equal(resp.GetBin(), []byte(conn.LocalAddr().String())) {
t.Fatalf("expected response to be an address %s, got %s", conn.LocalAddr().String(), resp.GetBin())
if result != "hello" {
t.Fatalf("expected response to be a hello, got %s", result)
}
cancel()
<-errChan
Expand Down
82 changes: 24 additions & 58 deletions thrift/lib/go/thrift/rocket_simple_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,15 @@
package thrift

import (
"bytes"
"context"
"net"
"testing"
"time"

"github.com/facebook/fbthrift/thrift/lib/go/thrift/dummy"
"github.com/facebook/fbthrift/thrift/lib/go/thrift/types"
"github.com/facebook/fbthrift/thrift/lib/thrift/metadata"
)

type rocketServerTestProcessor struct {
requests chan<- *MyTestStruct
}

func (t *rocketServerTestProcessor) ProcessorFunctionMap() map[string]types.ProcessorFunction {
return map[string]types.ProcessorFunction{"test": &rocketServerTestProcessorFunction{&testProcessorFunction{}, t.requests}}
}

func (t *rocketServerTestProcessor) GetThriftMetadata() *metadata.ThriftMetadata {
return nil
}

type rocketServerTestProcessorFunction struct {
types.ProcessorFunction
requests chan<- *MyTestStruct
}

func (p *rocketServerTestProcessorFunction) RunContext(ctx context.Context, reqStruct types.Struct) (types.WritableStruct, types.ApplicationException) {
v, ok := ConnInfoFromContext(ctx)
if ok {
reqStruct.(*MyTestStruct).Bin = []byte(v.RemoteAddr.String())
}
if p.requests != nil {
p.requests <- reqStruct.(*MyTestStruct)
}
return reqStruct, nil
}

// Make sure that ConnInfo is added to the context of a rocket server.
func TestRocketServerConnInfo(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -65,7 +36,8 @@ func TestRocketServerConnInfo(t *testing.T) {
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
server := NewSimpleServer(&rocketServerTestProcessor{}, listener, TransportIDRocket)
processor := dummy.NewDummyProcessor(&dummy.DummyHandler{})
server := NewSimpleServer(processor, listener, TransportIDRocket)
go func() {
errChan <- server.ServeContext(ctx)
}()
Expand All @@ -78,19 +50,14 @@ func TestRocketServerConnInfo(t *testing.T) {
if err != nil {
t.Fatalf("could not create client protocol: %s", err)
}
client := NewSerialChannel(proto)
req := &MyTestStruct{
St: "hello",
}
resp := &MyTestStruct{}
if err := client.Call(context.Background(), "test", req, resp); err != nil {
client := dummy.NewDummyChannelClient(NewSerialChannel(proto))
defer client.Close()
result, err := client.Echo(context.TODO(), "hello")
if err != nil {
t.Fatalf("could not complete call: %v", err)
}
if resp.St != "hello" {
t.Fatalf("expected response to be a hello, got %s", resp.St)
}
if !bytes.Equal(resp.GetBin(), []byte(conn.LocalAddr().String())) {
t.Fatalf("expected response to be an address %s, got %s", conn.LocalAddr().String(), resp.GetBin())
if result != "hello" {
t.Fatalf("expected response to be a hello, got %s", result)
}
cancel()
<-errChan
Expand All @@ -106,8 +73,9 @@ func TestRocketServerOneWay(t *testing.T) {
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
received := make(chan *MyTestStruct)
server := NewSimpleServer(&rocketServerTestProcessor{received}, listener, TransportIDRocket)
received := make(chan string)
processor := dummy.NewDummyProcessor(&dummy.DummyHandler{OnewayRPCRequests: received})
server := NewSimpleServer(processor, listener, TransportIDRocket)
go func() {
errChan <- server.ServeContext(ctx)
}()
Expand All @@ -120,11 +88,10 @@ func TestRocketServerOneWay(t *testing.T) {
if err != nil {
t.Fatalf("could not create client protocol: %s", err)
}
client := NewSerialChannel(proto)
req := &MyTestStruct{
St: "hello",
}
if err := client.Oneway(context.Background(), "test", req); err != nil {
client := dummy.NewDummyChannelClient(NewSerialChannel(proto))
defer client.Close()
err = client.OnewayRPC(context.TODO(), "hello")
if err != nil {
t.Fatalf("could not complete call: %v", err)
}
<-received
Expand All @@ -142,7 +109,8 @@ func TestRocketServerCloseListener(t *testing.T) {
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
server := NewSimpleServer(&rocketServerTestProcessor{}, listener, TransportIDRocket)
processor := dummy.NewDummyProcessor(&dummy.DummyHandler{})
server := NewSimpleServer(processor, listener, TransportIDRocket)
go func() {
errChan <- server.ServeContext(ctx)
}()
Expand All @@ -155,16 +123,14 @@ func TestRocketServerCloseListener(t *testing.T) {
if err != nil {
t.Fatalf("could not create client protocol: %s", err)
}
client := NewSerialChannel(proto)
req := &MyTestStruct{
St: "hello",
}
resp := &MyTestStruct{}
if err := client.Call(context.Background(), "test", req, resp); err != nil {
client := dummy.NewDummyChannelClient(NewSerialChannel(proto))
defer client.Close()
result, err := client.Echo(context.TODO(), "hello")
if err != nil {
t.Fatalf("could not complete call: %v", err)
}
if resp.St != "hello" {
t.Fatalf("expected response to be a hello, got %s", resp.St)
if result != "hello" {
t.Fatalf("expected response to be a hello, got %s", result)
}
listener.Close()
select {
Expand Down
18 changes: 9 additions & 9 deletions thrift/lib/go/thrift/rocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net"
"testing"

"github.com/facebook/fbthrift/thrift/lib/go/thrift/dummy"
"github.com/facebook/fbthrift/thrift/lib/go/thrift/types"
)

Expand All @@ -34,7 +35,8 @@ func TestRocket(t *testing.T) {
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
server := NewSimpleServer(&testProcessor{}, listener, TransportIDRocket)
processor := dummy.NewDummyProcessor(&dummy.DummyHandler{})
server := NewSimpleServer(processor, listener, TransportIDRocket)
go func() {
errChan <- server.ServeContext(ctx)
}()
Expand All @@ -47,16 +49,14 @@ func TestRocket(t *testing.T) {
if err != nil {
t.Fatalf("could not create client protocol: %s", err)
}
client := NewSerialChannel(proto)
req := &MyTestStruct{
St: "hello",
}
resp := &MyTestStruct{}
if err := client.Call(context.Background(), "test", req, resp); err != nil {
client := dummy.NewDummyChannelClient(NewSerialChannel(proto))
defer client.Close()
result, err := client.Echo(context.TODO(), "hello")
if err != nil {
t.Fatalf("could not complete call: %v", err)
}
if resp.St != "hello" {
t.Fatalf("expected response to be %s, got %s", "hello", resp.St)
if result != "hello" {
t.Fatalf("expected response to be a hello, got %s", result)
}
cancel()
<-errChan
Expand Down
Loading

0 comments on commit 7f33f57

Please sign in to comment.