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

Function TestPeerStringer

peer/peer_test.go:49–101  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

47func (a *addr) String() string { return a.ipAddress }
48
49func TestPeerStringer(t *testing.T) {
50 testCases := []struct {
51 name string
52 peer *Peer
53 want string
54 }{
55 {
56 name: "+Addr-LocalAddr+ValidAuth",
57 peer: &Peer{Addr: &addr{"example.com:1234"}, AuthInfo: testAuthInfo{credentials.CommonAuthInfo{SecurityLevel: credentials.PrivacyAndIntegrity}}},
58 want: "Peer{Addr: 'example.com:1234', LocalAddr: <nil>, AuthInfo: 'testAuthInfo-3'}",
59 },
60 {
61 name: "+Addr+LocalAddr+ValidAuth",
62 peer: &Peer{Addr: &addr{"example.com:1234"}, LocalAddr: &addr{"example.com:1234"}, AuthInfo: testAuthInfo{credentials.CommonAuthInfo{SecurityLevel: credentials.PrivacyAndIntegrity}}},
63 want: "Peer{Addr: 'example.com:1234', LocalAddr: 'example.com:1234', AuthInfo: 'testAuthInfo-3'}",
64 },
65 {
66 name: "+Addr-LocalAddr+emptyAuth",
67 peer: &Peer{Addr: &addr{"1.2.3.4:1234"}, AuthInfo: testAuthInfo{credentials.CommonAuthInfo{}}},
68 want: "Peer{Addr: '1.2.3.4:1234', LocalAddr: <nil>, AuthInfo: 'testAuthInfo-0'}",
69 },
70 {
71 name: "-Addr-LocalAddr+emptyAuth",
72 peer: &Peer{AuthInfo: testAuthInfo{}},
73 want: "Peer{Addr: <nil>, LocalAddr: <nil>, AuthInfo: 'testAuthInfo-0'}",
74 },
75 {
76 name: "zeroedPeer",
77 peer: &Peer{},
78 want: "Peer{Addr: <nil>, LocalAddr: <nil>, AuthInfo: <nil>}",
79 },
80 {
81 name: "nilPeer",
82 peer: nil,
83 want: "Peer<nil>",
84 },
85 }
86 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
87 defer cancel()
88 for _, tc := range testCases {
89 t.Run(tc.name, func(t *testing.T) {
90 ctx = NewContext(ctx, tc.peer)
91
92 p, ok := FromContext(ctx)
93 if !ok {
94 t.Fatalf("Unable to get peer from context")
95 }
96 if p.String() != tc.want {
97 t.Fatalf("Error using peer String(): expected %q, got %q", tc.want, p.String())
98 }
99 })
100 }
101}

Callers

nothing calls this directly

Calls 4

NewContextFunction · 0.85
FromContextFunction · 0.85
FatalfMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected