MCPcopy
hub / github.com/grpc/grpc-go / checkBufferWasWrittenAsExpected

Function checkBufferWasWrittenAsExpected

grpclog/internal/loggerv2_test.go:61–90  ·  view source on GitHub ↗

checkBufferWasWrittenAsExpected checks that the log buffer buf was written as expected, per the discard, logTYpe, msg, and isJSON arguments.

(t *testing.T, buf *bytes.Buffer, discard bool, logType string, msg string, isJSON bool)

Source from the content-addressed store, hash-verified

59// checkBufferWasWrittenAsExpected checks that the log buffer buf was written as expected,
60// per the discard, logTYpe, msg, and isJSON arguments.
61func checkBufferWasWrittenAsExpected(t *testing.T, buf *bytes.Buffer, discard bool, logType string, msg string, isJSON bool) {
62 bts, err := buf.ReadBytes('\n')
63 if discard {
64 if err == nil {
65 t.Fatalf("got '%v', want discard %v to not write", string(bts), logType)
66 } else if err != io.EOF {
67 t.Fatalf("got '%v', want discard %v buffer to be EOF", err, logType)
68 }
69 } else {
70 if err != nil {
71 t.Fatalf("got '%v', want non-discard %v to not error", err, logType)
72 } else if !bytes.Contains(bts, []byte(msg)) {
73 t.Fatalf("got '%v', want non-discard %v buffer contain message '%v'", string(bts), logType, msg)
74 }
75 if isJSON {
76 obj := map[string]string{}
77 if err := json.Unmarshal(bts, &obj); err != nil {
78 t.Fatalf("got '%v', want non-discard json %v to unmarshal", err, logType)
79 } else if _, ok := obj["severity"]; !ok {
80 t.Fatalf("got '%v', want non-discard json %v to have severity field", "missing severity", logType)
81
82 } else if jsonMsg, ok := obj["message"]; !ok {
83 t.Fatalf("got '%v', want non-discard json %v to have message field", "missing message", logType)
84
85 } else if !strings.Contains(jsonMsg, msg) {
86 t.Fatalf("got '%v', want non-discard json %v buffer contain message '%v'", string(bts), logType, msg)
87 }
88 }
89 }
90}
91
92// check if b is in the format of:
93//

Callers 2

TestLoggerV2FatalFunction · 0.85

Calls 2

FatalfMethod · 0.65
UnmarshalMethod · 0.65

Tested by

no test coverage detected