Skip to content

Commit e7f034e

Browse files
committed
add multigen test
1 parent 93afc3b commit e7f034e

File tree

4 files changed

+393
-0
lines changed

4 files changed

+393
-0
lines changed

internal/circuitgentest/circuittestmulti/aggregator.gen.go

+106
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2019 Twitch Interactive, Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
4+
// use this file except in compliance with the License. A copy of the License is
5+
// located at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed on
10+
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package circuittestmulti
15+
16+
// Test generation in a separate package. gen_test.go contains comprehensive test on generated circuit wrappers.
17+
//
18+
// Disable goimports to catch any import bugs
19+
20+
//go:generate circuitgen circuit --goimports=true --pkg ../ --name Publisher --name Aggregator --out ./
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright 2019 Twitch Interactive, Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
4+
// use this file except in compliance with the License. A copy of the License is
5+
// located at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed on
10+
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package circuittestmulti
15+
16+
import (
17+
"context"
18+
"errors"
19+
"testing"
20+
"time"
21+
22+
"github.com/cep21/circuit"
23+
"github.com/stretchr/testify/mock"
24+
"github.com/stretchr/testify/require"
25+
"github.com/twitchtv/circuitgen/internal/circuitgentest"
26+
"github.com/twitchtv/circuitgen/internal/circuitgentest/rep"
27+
)
28+
29+
// Thinner test of multi-gen interface
30+
func TestPublisherInterface(t *testing.T) {
31+
manager := &circuit.Manager{}
32+
33+
m := &circuitgentest.MockPublisher{}
34+
m.On("PublishWithResult", mock.Anything, mock.Anything).Return(nil, nil).Once()
35+
m.On("Publish", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, nil).Once()
36+
m.On("Close", mock.Anything).Return(nil).Once()
37+
38+
publisher, err := NewCircuitWrapperPublisher(manager, m, CircuitWrapperPublisherConfig{})
39+
require.NoError(t, err)
40+
require.NotNil(t, publisher)
41+
42+
// Check circuit names
43+
names := circuitNames(manager)
44+
require.Contains(t, names, "Publisher.PublishWithResult")
45+
require.Contains(t, names, "Publisher.Publish")
46+
47+
ctx := context.Background()
48+
_, err = publisher.Publish(ctx, map[circuitgentest.Seed][][]circuitgentest.Grant{}, circuitgentest.TopicsList{})
49+
require.NoError(t, err)
50+
51+
_, err = publisher.PublishWithResult(ctx, rep.PublishInput{})
52+
require.NoError(t, err)
53+
54+
require.NoError(t, publisher.Close())
55+
56+
// Check embedded called
57+
m.AssertExpectations(t)
58+
}
59+
60+
61+
func TestAggregatorStruct(t *testing.T) {
62+
manager := &circuit.Manager{}
63+
agg := &circuitgentest.Aggregator{}
64+
65+
incSumCounter := &runMetricsCounter{}
66+
wrapperAgg, err := NewCircuitWrapperAggregator(manager, agg, CircuitWrapperAggregatorConfig{
67+
CircuitIncSum: circuit.Config{
68+
Metrics: circuit.MetricsCollectors{
69+
Run: []circuit.RunMetrics{incSumCounter},
70+
},
71+
},
72+
})
73+
require.NoError(t, err)
74+
75+
err = wrapperAgg.IncSum(context.Background(), 10)
76+
require.NoError(t, err)
77+
require.Equal(t, 10, agg.Sum())
78+
require.Equal(t, 10, wrapperAgg.Sum())
79+
require.Equal(t, 1, incSumCounter.success)
80+
81+
sumErr := errors.New("sum error")
82+
agg.IncSumError = sumErr
83+
err = wrapperAgg.IncSum(context.Background(), 10)
84+
require.Equal(t, sumErr, err)
85+
86+
}
87+
88+
func circuitNames(m *circuit.Manager) []string {
89+
names := make([]string, 0, len(m.AllCircuits()))
90+
for _, circ := range m.AllCircuits() {
91+
names = append(names, circ.Name())
92+
}
93+
return names
94+
}
95+
96+
type runMetricsCounter struct {
97+
success int
98+
failure int
99+
timeout int
100+
badRequest int
101+
interrupt int
102+
concurrencyLimitReject int
103+
shortCircuit int
104+
}
105+
106+
func (r *runMetricsCounter) Success(now time.Time, duration time.Duration) { r.success++ }
107+
func (r *runMetricsCounter) ErrFailure(now time.Time, duration time.Duration) { r.failure++ }
108+
func (r *runMetricsCounter) ErrTimeout(now time.Time, duration time.Duration) { r.timeout++ }
109+
func (r *runMetricsCounter) ErrBadRequest(now time.Time, duration time.Duration) { r.badRequest++ }
110+
func (r *runMetricsCounter) ErrInterrupt(now time.Time, duration time.Duration) { r.interrupt++ }
111+
func (r *runMetricsCounter) ErrConcurrencyLimitReject(now time.Time) { r.concurrencyLimitReject++ }
112+
func (r *runMetricsCounter) ErrShortCircuit(now time.Time) { r.shortCircuit++ }
113+
114+
var _ circuit.RunMetrics = (*runMetricsCounter)(nil)

internal/circuitgentest/circuittestmulti/publisher.gen.go

+153
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)