Skip to content

Commit

Permalink
Added unit tests in internal/tracegen (#5119)
Browse files Browse the repository at this point in the history
- Partially fixes #5068

- This commit adds tests for the `Run` function defined in the `internal/tracegen` package.

- make test

- [x] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
- [x] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `yarn lint` and `yarn test`

Signed-off-by: VaibhavMalik4187 <[email protected]>
  • Loading branch information
VaibhavMalik4187 committed Jan 20, 2024
1 parent 88efe47 commit 8be267d
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
105 changes: 105 additions & 0 deletions internal/tracegen/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package tracegen

import (
"errors"
"flag"
"testing"

"github.com/stretchr/testify/assert"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)

func Test_Run(t *testing.T) {
logger := zap.NewNop()
tp := sdktrace.NewTracerProvider()

tests := []struct {
name string
config *Config
expectedErr error
}{
{
name: "Empty config",
config: &Config{},
expectedErr: errors.New("either `traces` or `duration` must be greater than 0"),
},
{
name: "Non-empty config",
config: &Config{
Workers: 2,
Traces: 10,
ChildSpans: 5,
Attributes: 20,
AttrKeys: 50,
AttrValues: 100,
},
expectedErr: nil,
},
{
name: "Traces and duration set to 0",
config: &Config{
Traces: 0,
Duration: 0,
},
expectedErr: errors.New("either `traces` or `duration` must be greater than 0"),
},
{
name: "Negative traces, positive duration",
config: &Config{
Traces: -7,
Duration: 7,
},
expectedErr: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tracers := []trace.Tracer{tp.Tracer("Test-Tracer")}
err := Run(tt.config, tracers, logger)
assert.Equal(t, tt.expectedErr, err)
})
}
}

func Test_Flags(t *testing.T) {
tests := []struct {
name string
fs *flag.FlagSet
config *Config
expectedConfig *Config
}{
{
fs: &flag.FlagSet{},
name: "Non-empty config",
config: &Config{},
expectedConfig: &Config{
Workers: 1,
Traces: 1,
ChildSpans: 1,
Attributes: 11,
AttrKeys: 97,
AttrValues: 1000,
Debug: false,
Firehose: false,
Pause: 1000,
Duration: 0,
Service: "tracegen",
Services: 1,
TraceExporter: "otlp-http",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.config.Flags(tt.fs)
assert.Equal(t, tt.expectedConfig, tt.config)
})
}
}
57 changes: 57 additions & 0 deletions internal/tracegen/worker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package tracegen

import (
"sync"
"testing"
"time"

"github.com/jaegertracing/jaeger/pkg/testutils"
"github.com/stretchr/testify/assert"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)

func Test_SimulateTraces(t *testing.T) {
logger, buf := testutils.NewLogger()
tp := sdktrace.NewTracerProvider()
tracers := []trace.Tracer{tp.Tracer("stdout")}
wg := sync.WaitGroup{}
wg.Add(1)
var running uint32 = 1

tests := []struct {
name string
worker *worker
expectedOutput string
}{
{
name: "stdout tracer",
worker: &worker{
logger: logger,
tracers: tracers,
wg: &wg,
id: 7,
running: &running,
Config: Config{
Traces: 7,
Duration: time.Second,
Pause: time.Second,
Service: "stdout",
Debug: true,
Firehose: true,
},
},
expectedOutput: "{\"level\":\"info\",\"msg\":\"Worker 7 generated 7 traces\"}\n",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.worker.simulateTraces()
assert.Equal(t, tt.expectedOutput, buf.String())
})
}
}

0 comments on commit 8be267d

Please sign in to comment.