From ca93e3f3cda344e3ecc5cb46df29c7e363033825 Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Tue, 14 Mar 2023 13:23:44 -0700 Subject: [PATCH] server: remove the Run helper This has no meaningful use to justify its existence in the API. Updates #46. --- server/example_test.go | 18 ------------ server/run.go | 25 ----------------- server/run_test.go | 64 ------------------------------------------ 3 files changed, 107 deletions(-) delete mode 100644 server/run.go delete mode 100644 server/run_test.go diff --git a/server/example_test.go b/server/example_test.go index 43573840..081ecb4d 100644 --- a/server/example_test.go +++ b/server/example_test.go @@ -8,7 +8,6 @@ import ( "log" "github.com/creachadair/jrpc2" - "github.com/creachadair/jrpc2/channel" "github.com/creachadair/jrpc2/handler" "github.com/creachadair/jrpc2/server" ) @@ -47,20 +46,3 @@ func (s Service) Finish(_ jrpc2.Assigner, stat jrpc2.ServerStatus) { fmt.Printf("SERVICE FINISHED err=%v\n", stat.Err) close(s.done) } - -func ExampleRun() { - done := make(chan struct{}) - cch, sch := channel.Direct() - go server.Run(sch, Service{done}, nil) - - cli := jrpc2.NewClient(cch, nil) - if _, err := cli.Call(context.Background(), "Hello", nil); err != nil { - log.Fatalf("Call failed: %v", err) - } - cli.Close() - <-done - // Output: - // SERVICE STARTED - // Hello human - // SERVICE FINISHED err= -} diff --git a/server/run.go b/server/run.go deleted file mode 100644 index 90a603e5..00000000 --- a/server/run.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (C) 2017 Michael J. Fromberger. All Rights Reserved. - -package server - -import ( - "github.com/creachadair/jrpc2" - "github.com/creachadair/jrpc2/channel" -) - -// Run starts a server for svc on the given channel, and blocks until it -// returns. The server exit status is reported to the service, and the error -// value is returned. -// -// If the caller does not need the error value and does not want to wait for -// the server to complete, call Run in a goroutine. -func Run(ch channel.Channel, svc Service, opts *jrpc2.ServerOptions) error { - assigner, err := svc.Assigner() - if err != nil { - return err - } - srv := jrpc2.NewServer(assigner, opts).Start(ch) - stat := srv.WaitStatus() - svc.Finish(assigner, stat) - return stat.Err -} diff --git a/server/run_test.go b/server/run_test.go deleted file mode 100644 index 822feff8..00000000 --- a/server/run_test.go +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright (C) 2017 Michael J. Fromberger. All Rights Reserved. - -package server_test - -import ( - "context" - "testing" - - "github.com/creachadair/jrpc2" - "github.com/creachadair/jrpc2/channel" - "github.com/creachadair/jrpc2/handler" - "github.com/creachadair/jrpc2/server" -) - -type testService struct { - assigner jrpc2.Assigner - assignCalled, finishCalled bool - stat jrpc2.ServerStatus -} - -func (t *testService) Assigner() (jrpc2.Assigner, error) { - t.assignCalled = true - return t.assigner, nil -} - -func (t *testService) Finish(_ jrpc2.Assigner, stat jrpc2.ServerStatus) { - t.finishCalled = true - t.stat = stat -} - -func TestRun(t *testing.T) { - svc := &testService{assigner: handler.Map{ - "Test": handler.New(func(ctx context.Context) string { - return "OK" - }), - }} - cpipe, spipe := channel.Direct() - cdone := make(chan struct{}) - var result string - go func() { - defer close(cdone) - cli := jrpc2.NewClient(cpipe, nil) - defer cli.Close() - if err := cli.CallResult(context.Background(), "Test", nil, &result); err != nil { - t.Errorf("Call Test failed: %v", err) - } - }() - - if err := server.Run(spipe, svc, nil); err != nil { - t.Errorf("Server failed: %v", err) - } - if result != "OK" { - t.Errorf("Call result: got %q, want %q", result, "OK") - } - if !svc.assignCalled { - t.Error("Assigner was not called") - } - if !svc.finishCalled { - t.Error("Finish was not called") - } - if svc.stat.Err != nil { - t.Errorf("Server status: unexpected error: %+v", svc.stat) - } -}