noop_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright The OpenTelemetry 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 trace
  15. import (
  16. "context"
  17. "testing"
  18. )
  19. func TestNewNoopTracerProvider(t *testing.T) {
  20. got, want := NewNoopTracerProvider(), noopTracerProvider{}
  21. if got != want {
  22. t.Errorf("NewNoopTracerProvider() returned %#v, want %#v", got, want)
  23. }
  24. }
  25. func TestNoopTracerProviderTracer(t *testing.T) {
  26. tp := NewNoopTracerProvider()
  27. got, want := tp.Tracer(""), noopTracer{}
  28. if got != want {
  29. t.Errorf("noopTracerProvider.Tracer() returned %#v, want %#v", got, want)
  30. }
  31. }
  32. func TestNoopTracerStart(t *testing.T) {
  33. ctx := context.Background()
  34. tracer := NewNoopTracerProvider().Tracer("test instrumentation")
  35. var span Span
  36. ctx, span = tracer.Start(ctx, "span name")
  37. got, ok := span.(noopSpan)
  38. if !ok {
  39. t.Fatalf("noopTracer.Start() returned a non-noopSpan: %#v", span)
  40. }
  41. want := noopSpan{}
  42. if got != want {
  43. t.Errorf("noopTracer.Start() returned %#v, want %#v", got, want)
  44. }
  45. got, ok = SpanFromContext(ctx).(noopSpan)
  46. if !ok {
  47. t.Fatal("noopTracer.Start() did not set span as current in returned context")
  48. }
  49. if got != want {
  50. t.Errorf("noopTracer.Start() current span in returned context set to %#v, want %#v", got, want)
  51. }
  52. }
  53. func TestNoopSpan(t *testing.T) {
  54. tracer := NewNoopTracerProvider().Tracer("test instrumentation")
  55. _, s := tracer.Start(context.Background(), "test span")
  56. span := s.(noopSpan)
  57. if got, want := span.SpanContext(), (SpanContext{}); !assertSpanContextEqual(got, want) {
  58. t.Errorf("span.SpanContext() returned %#v, want %#v", got, want)
  59. }
  60. if got, want := span.IsRecording(), false; got != want {
  61. t.Errorf("span.IsRecording() returned %#v, want %#v", got, want)
  62. }
  63. }
  64. func TestNonRecordingSpanTracerStart(t *testing.T) {
  65. tid, err := TraceIDFromHex("01000000000000000000000000000000")
  66. if err != nil {
  67. t.Fatalf("failure creating TraceID: %s", err.Error())
  68. }
  69. sid, err := SpanIDFromHex("0200000000000000")
  70. if err != nil {
  71. t.Fatalf("failure creating SpanID: %s", err.Error())
  72. }
  73. sc := NewSpanContext(SpanContextConfig{TraceID: tid, SpanID: sid})
  74. ctx := ContextWithSpanContext(context.Background(), sc)
  75. _, span := NewNoopTracerProvider().Tracer("test instrumentation").Start(ctx, "span1")
  76. if got, want := span.SpanContext(), sc; !assertSpanContextEqual(got, want) {
  77. t.Errorf("SpanContext not carried by nonRecordingSpan. got %#v, want %#v", got, want)
  78. }
  79. }