-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Before this commit, a custom logger could be set with the LogMessages function. However, by using LogMessages not only is a custom logger set but allso all received and sent messages are logged. Use cases exist where a custom logger is desired to log error but not all messages.
- Loading branch information
1 parent
99f63e0
commit b3b263a
Showing
2 changed files
with
60 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package jsonrpc2_test | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"io" | ||
"log" | ||
"net" | ||
"testing" | ||
|
||
"github.com/sourcegraph/jsonrpc2" | ||
) | ||
|
||
func TestSetLogger(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
rd, wr := io.Pipe() | ||
defer rd.Close() | ||
defer wr.Close() | ||
|
||
buf := bufio.NewReader(rd) | ||
logger := log.New(wr, "", log.Lmsgprefix) | ||
|
||
a, b := net.Pipe() | ||
connA := jsonrpc2.NewConn( | ||
ctx, | ||
jsonrpc2.NewBufferedStream(a, jsonrpc2.VSCodeObjectCodec{}), | ||
noopHandler{}, | ||
jsonrpc2.SetLogger(logger), | ||
) | ||
connB := jsonrpc2.NewConn( | ||
ctx, | ||
jsonrpc2.NewBufferedStream(b, jsonrpc2.VSCodeObjectCodec{}), | ||
noopHandler{}, | ||
) | ||
defer connA.Close() | ||
defer connB.Close() | ||
|
||
// Write a response with no corresponding request. | ||
if err := connB.Reply(ctx, jsonrpc2.ID{Num: 0}, nil); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := "jsonrpc2: ignoring response #0 with no corresponding request\n" | ||
got, err := buf.ReadString('\n') | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if got != want { | ||
t.Fatalf("got %q, want %q", got, want) | ||
} | ||
} |