You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

234 lines
5.6 KiB

  1. // Copyright 2018, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ocgrpc_test
  15. import (
  16. "io"
  17. "testing"
  18. "time"
  19. "go.opencensus.io/internal/testpb"
  20. "go.opencensus.io/trace"
  21. "golang.org/x/net/context"
  22. )
  23. type testExporter struct {
  24. ch chan *trace.SpanData
  25. }
  26. func (t *testExporter) ExportSpan(s *trace.SpanData) {
  27. go func() { t.ch <- s }()
  28. }
  29. func TestStreaming(t *testing.T) {
  30. trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
  31. te := testExporter{make(chan *trace.SpanData)}
  32. trace.RegisterExporter(&te)
  33. defer trace.UnregisterExporter(&te)
  34. client, cleanup := testpb.NewTestClient(t)
  35. stream, err := client.Multiple(context.Background())
  36. if err != nil {
  37. t.Fatalf("Call failed: %v", err)
  38. }
  39. err = stream.Send(&testpb.FooRequest{})
  40. if err != nil {
  41. t.Fatalf("Couldn't send streaming request: %v", err)
  42. }
  43. stream.CloseSend()
  44. for {
  45. _, err := stream.Recv()
  46. if err == io.EOF {
  47. break
  48. }
  49. if err != nil {
  50. t.Errorf("stream.Recv() = %v; want no errors", err)
  51. }
  52. }
  53. cleanup()
  54. s1 := <-te.ch
  55. s2 := <-te.ch
  56. checkSpanData(t, s1, s2, "testpb.Foo.Multiple", true)
  57. select {
  58. case <-te.ch:
  59. t.Fatal("received extra exported spans")
  60. case <-time.After(time.Second / 10):
  61. }
  62. }
  63. func TestStreamingFail(t *testing.T) {
  64. trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
  65. te := testExporter{make(chan *trace.SpanData)}
  66. trace.RegisterExporter(&te)
  67. defer trace.UnregisterExporter(&te)
  68. client, cleanup := testpb.NewTestClient(t)
  69. stream, err := client.Multiple(context.Background())
  70. if err != nil {
  71. t.Fatalf("Call failed: %v", err)
  72. }
  73. err = stream.Send(&testpb.FooRequest{Fail: true})
  74. if err != nil {
  75. t.Fatalf("Couldn't send streaming request: %v", err)
  76. }
  77. stream.CloseSend()
  78. for {
  79. _, err := stream.Recv()
  80. if err == nil || err == io.EOF {
  81. t.Errorf("stream.Recv() = %v; want errors", err)
  82. } else {
  83. break
  84. }
  85. }
  86. s1 := <-te.ch
  87. s2 := <-te.ch
  88. checkSpanData(t, s1, s2, "testpb.Foo.Multiple", false)
  89. cleanup()
  90. select {
  91. case <-te.ch:
  92. t.Fatal("received extra exported spans")
  93. case <-time.After(time.Second / 10):
  94. }
  95. }
  96. func TestSingle(t *testing.T) {
  97. trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
  98. te := testExporter{make(chan *trace.SpanData)}
  99. trace.RegisterExporter(&te)
  100. defer trace.UnregisterExporter(&te)
  101. client, cleanup := testpb.NewTestClient(t)
  102. _, err := client.Single(context.Background(), &testpb.FooRequest{})
  103. if err != nil {
  104. t.Fatalf("Couldn't send request: %v", err)
  105. }
  106. s1 := <-te.ch
  107. s2 := <-te.ch
  108. checkSpanData(t, s1, s2, "testpb.Foo.Single", true)
  109. cleanup()
  110. select {
  111. case <-te.ch:
  112. t.Fatal("received extra exported spans")
  113. case <-time.After(time.Second / 10):
  114. }
  115. }
  116. func TestServerSpanDuration(t *testing.T) {
  117. client, cleanup := testpb.NewTestClient(t)
  118. defer cleanup()
  119. te := testExporter{make(chan *trace.SpanData, 100)}
  120. trace.RegisterExporter(&te)
  121. defer trace.UnregisterExporter(&te)
  122. trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
  123. ctx := context.Background()
  124. const sleep = 100 * time.Millisecond
  125. client.Single(ctx, &testpb.FooRequest{SleepNanos: int64(sleep)})
  126. loop:
  127. for {
  128. select {
  129. case span := <-te.ch:
  130. if span.SpanKind != trace.SpanKindServer {
  131. continue loop
  132. }
  133. if got, want := span.EndTime.Sub(span.StartTime), sleep; got < want {
  134. t.Errorf("span duration = %dns; want at least %dns", got, want)
  135. }
  136. break loop
  137. default:
  138. t.Fatal("no more spans")
  139. }
  140. }
  141. }
  142. func TestSingleFail(t *testing.T) {
  143. trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
  144. te := testExporter{make(chan *trace.SpanData)}
  145. trace.RegisterExporter(&te)
  146. defer trace.UnregisterExporter(&te)
  147. client, cleanup := testpb.NewTestClient(t)
  148. _, err := client.Single(context.Background(), &testpb.FooRequest{Fail: true})
  149. if err == nil {
  150. t.Fatalf("Got nil error from request, want non-nil")
  151. }
  152. s1 := <-te.ch
  153. s2 := <-te.ch
  154. checkSpanData(t, s1, s2, "testpb.Foo.Single", false)
  155. cleanup()
  156. select {
  157. case <-te.ch:
  158. t.Fatal("received extra exported spans")
  159. case <-time.After(time.Second / 10):
  160. }
  161. }
  162. func checkSpanData(t *testing.T, s1, s2 *trace.SpanData, methodName string, success bool) {
  163. t.Helper()
  164. if s1.SpanKind == trace.SpanKindServer {
  165. s1, s2 = s2, s1
  166. }
  167. if got, want := s1.Name, methodName; got != want {
  168. t.Errorf("Got name %q want %q", got, want)
  169. }
  170. if got, want := s2.Name, methodName; got != want {
  171. t.Errorf("Got name %q want %q", got, want)
  172. }
  173. if got, want := s2.SpanContext.TraceID, s1.SpanContext.TraceID; got != want {
  174. t.Errorf("Got trace IDs %s and %s, want them equal", got, want)
  175. }
  176. if got, want := s2.ParentSpanID, s1.SpanContext.SpanID; got != want {
  177. t.Errorf("Got ParentSpanID %s, want %s", got, want)
  178. }
  179. if got := (s1.Status.Code == 0); got != success {
  180. t.Errorf("Got success=%t want %t", got, success)
  181. }
  182. if got := (s2.Status.Code == 0); got != success {
  183. t.Errorf("Got success=%t want %t", got, success)
  184. }
  185. if s1.HasRemoteParent {
  186. t.Errorf("Got HasRemoteParent=%t, want false", s1.HasRemoteParent)
  187. }
  188. if !s2.HasRemoteParent {
  189. t.Errorf("Got HasRemoteParent=%t, want true", s2.HasRemoteParent)
  190. }
  191. }