Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to create connector.*Routers for tests #7672

Closed
mwear opened this issue May 11, 2023 · 2 comments
Closed

add ability to create connector.*Routers for tests #7672

mwear opened this issue May 11, 2023 · 2 comments

Comments

@mwear
Copy link
Member

mwear commented May 11, 2023

In order to test routing based connectors we need to be able to construct connector.*Routers in order to build connectors via their factory methods. The connector internals handles this for us in the real world.

As suggested in open-telemetry/opentelemetry-collector-contrib#21498 (comment) we can move the fanoutconsumer from service/internal to internal and add methods to aid construction of connector.*Routers in connectortest.

Please assign this to me. I have PR to add this functionality that I will open shortly.

codeboten pushed a commit that referenced this issue Jun 22, 2023
This PR adds helpers to `connectortest` to aid the construction of
`connector.*Router`s for testing connectors. This was implemented based
on @djaglowski's [comment
here](open-telemetry/opentelemetry-collector-contrib#21498 (comment)),
with some slight modifications. I found it more ergonomic to pass the
sink into the `WithTracesSink` (and similar) options. You usually want a
handle on the sink after creating the router. While it's possible to get
the sink out of the router after the fact, it's a little cumbersome.

These helpers will be useful for
open-telemetry/opentelemetry-collector-contrib#21498
and future connectors that need use of routers.

For example, here is a test with the consumer passed in:

```go
func TestFanoutTracesWithSink(t *testing.T) {
    var sink0, sink1 consumertest.TracesSink

    tr, err := NewTracesRouterSink(
        WithTracesSink(component.NewIDWithName(component.DataTypeTraces, "0"), &sink0),
        WithTracesSink(component.NewIDWithName(component.DataTypeTraces, "1"), &sink1),
    )

    require.NoError(t, err)
    require.Equal(t, 0, sink0.SpanCount())
    require.Equal(t, 0, sink1.SpanCount())

    td := testdata.GenerateTraces(1)
    err = tr.(consumer.Traces).ConsumeTraces(context.Background(), td)

    require.NoError(t, err)
    require.Equal(t, 1, sink0.SpanCount())
    require.Equal(t, 1, sink1.SpanCount())
}

```

The same test having to extract the consumer out after the fact:

```go
func TestFanoutTracesWithSink(t *testing.T) {
    traces0 := component.NewIDWithName(component.DataTypeTraces, "0")
    traces1 := component.NewIDWithName(component.DataTypeTraces, "1")

    tr, err := NewTracesRouterSink(
        WithTracesSink(traces0),
        WithTracesSink(traces1),
    )
   
    require.NoError(t, err)
   
    cons0, _ := tr.Consumer(traces0)
    sink0 := cons0.(*consumertest.TracesSink)
    cons1, _ := tr.Consumer(traces1)
    sink1 := cons1.(*consumertest.TracesSink)

    require.Equal(t, 0, sink0.SpanCount())
    require.Equal(t, 0, sink1.SpanCount())

    td := testdata.GenerateTraces(1)
    err = tr.(consumer.Traces).ConsumeTraces(context.Background(), td)

    require.NoError(t, err)
    require.Equal(t, 1, sink0.SpanCount())
    require.Equal(t, 1, sink1.SpanCount())}
}
```


**Link to tracking Issue:** 
#7672

**Testing:**
Unit tests

**Documentation:**
Source code comments

---------

Co-authored-by: Daniel Jaglowski <[email protected]>
@atoulme
Copy link
Contributor

atoulme commented Dec 14, 2023

Can we close this issue as complete?

@mwear
Copy link
Member Author

mwear commented Dec 14, 2023

We can!

@mwear mwear closed this as completed Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants