github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/tracer/tests/implementation_test.go (about)

     1  // Copyright 2022 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  package tests
    14  
    15  import (
    16  	"context"
    17  	"testing"
    18  
    19  	"github.com/facebookincubator/go-belt/tool/experimental/tracer"
    20  )
    21  
    22  type DummyReporter interface {
    23  	OnSend(func(span tracer.Span))
    24  }
    25  
    26  type implementationCase struct {
    27  	Name    string
    28  	Factory func() (tracer.Tracer, DummyReporter)
    29  }
    30  
    31  var implementations []implementationCase
    32  
    33  func TestImplementations(t *testing.T) {
    34  	for _, implCase := range implementations {
    35  		t.Run(implCase.Name, func(t *testing.T) {
    36  			t.Run("case:parent-child-finish-finish", func(t *testing.T) {
    37  				tracerImpl, reporter := implCase.Factory()
    38  
    39  				checkParentChild := func(t *testing.T, parent, child tracer.Span) {
    40  					if tracer.IsNoopSpan(parent) {
    41  						t.Fatalf("the parent Span is NOOP")
    42  					}
    43  					if tracer.IsNoopSpan(child) {
    44  						t.Fatalf("the child Span is NOOP")
    45  					}
    46  					sendCount := 0
    47  					reporter.OnSend(func(span tracer.Span) {
    48  						sendCount++
    49  						if span.Name() != "child" {
    50  							t.Fatalf("unexpected name: %s", span.Name())
    51  						}
    52  					})
    53  					child.Finish()
    54  					child.Flush()
    55  					reporter.OnSend(func(span tracer.Span) {
    56  						sendCount++
    57  						if span.Name() != "parent" {
    58  							t.Fatalf("unexpected name: %s", span.Name())
    59  						}
    60  					})
    61  					parent.Finish()
    62  					parent.Flush()
    63  					if sendCount != 2 {
    64  						t.Fatalf("expected sendCount is 2, but got %d", sendCount)
    65  					}
    66  				}
    67  
    68  				t.Run("without_ctx", func(t *testing.T) {
    69  					parent := tracerImpl.Start("parent", nil)
    70  					child := tracerImpl.Start("child", parent)
    71  					checkParentChild(t, parent, child)
    72  				})
    73  
    74  				t.Run("with_ctx", func(t *testing.T) {
    75  					ctx := context.Background()
    76  					parent, ctx := tracerImpl.StartWithCtx(ctx, "parent")
    77  					child, ctx := tracerImpl.StartChildWithCtx(ctx, "child")
    78  					_ = ctx
    79  					checkParentChild(t, parent, child)
    80  				})
    81  			})
    82  		})
    83  	}
    84  }