Skip to content

Commit a079690

Browse files
committed
add multigen test
1 parent 9cd3b37 commit a079690

File tree

4 files changed

+394
-0
lines changed

4 files changed

+394
-0
lines changed

internal/circuitgentest/circuittest_multi/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 circuittest
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,115 @@
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 circuittest
15+
16+
import (
17+
"context"
18+
"errors"
19+
"testing"
20+
"time"
21+
22+
"github.com/cep21/circuit"
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/mock"
25+
"github.com/stretchr/testify/require"
26+
"github.com/twitchtv/circuitgen/internal/circuitgentest"
27+
"github.com/twitchtv/circuitgen/internal/circuitgentest/rep"
28+
)
29+
30+
// Thinner test of multi-gen interface
31+
func TestPublisherInterface(t *testing.T) {
32+
manager := &circuit.Manager{}
33+
34+
m := &circuitgentest.MockPublisher{}
35+
m.On("PublishWithResult", mock.Anything, mock.Anything).Return(nil, nil).Once()
36+
m.On("Publish", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, nil).Once()
37+
m.On("Close", mock.Anything).Return(nil).Once()
38+
39+
pubsub, err := NewCircuitWrapperPublisher(manager, m, CircuitWrapperPubsubConfig{})
40+
require.NoError(t, err)
41+
require.NotNil(t, pubsub)
42+
43+
// Check circuit names
44+
names := circuitNames(manager)
45+
require.Contains(t, names, "Publisher.PublishWithResult")
46+
require.Contains(t, names, "Publisher.Publish")
47+
48+
ctx := context.Background()
49+
_, err = pubsub.Publish(ctx, map[circuitgentest.Seed][][]circuitgentest.Grant{}, circuitgentest.TopicsList{})
50+
require.NoError(t, err)
51+
52+
_, err = pubsub.PublishWithResult(ctx, rep.PublishInput{})
53+
require.NoError(t, err)
54+
55+
require.NoError(t, pubsub.Close())
56+
57+
// Check embedded called
58+
m.AssertExpectations(t)
59+
}
60+
61+
62+
func TestAggregatorStruct(t *testing.T) {
63+
manager := &circuit.Manager{}
64+
agg := &circuitgentest.Aggregator{}
65+
66+
incSumCounter := &runMetricsCounter{}
67+
wrapperAgg, err := NewCircuitWrapperAggregator(manager, agg, CircuitWrapperAggregatorConfig{
68+
CircuitIncSum: circuit.Config{
69+
Metrics: circuit.MetricsCollectors{
70+
Run: []circuit.RunMetrics{incSumCounter},
71+
},
72+
},
73+
})
74+
require.NoError(t, err)
75+
76+
err = wrapperAgg.IncSum(context.Background(), 10)
77+
require.NoError(t, err)
78+
require.Equal(t, 10, agg.Sum())
79+
require.Equal(t, 10, wrapperAgg.Sum())
80+
require.Equal(t, 1, incSumCounter.success)
81+
82+
sumErr := errors.New("sum error")
83+
agg.IncSumError = sumErr
84+
err = wrapperAgg.IncSum(context.Background(), 10)
85+
require.Equal(t, sumErr, err)
86+
87+
}
88+
89+
func circuitNames(m *circuit.Manager) []string {
90+
names := make([]string, 0, len(m.AllCircuits()))
91+
for _, circ := range m.AllCircuits() {
92+
names = append(names, circ.Name())
93+
}
94+
return names
95+
}
96+
97+
type runMetricsCounter struct {
98+
success int
99+
failure int
100+
timeout int
101+
badRequest int
102+
interrupt int
103+
concurrencyLimitReject int
104+
shortCircuit int
105+
}
106+
107+
func (r *runMetricsCounter) Success(now time.Time, duration time.Duration) { r.success++ }
108+
func (r *runMetricsCounter) ErrFailure(now time.Time, duration time.Duration) { r.failure++ }
109+
func (r *runMetricsCounter) ErrTimeout(now time.Time, duration time.Duration) { r.timeout++ }
110+
func (r *runMetricsCounter) ErrBadRequest(now time.Time, duration time.Duration) { r.badRequest++ }
111+
func (r *runMetricsCounter) ErrInterrupt(now time.Time, duration time.Duration) { r.interrupt++ }
112+
func (r *runMetricsCounter) ErrConcurrencyLimitReject(now time.Time) { r.concurrencyLimitReject++ }
113+
func (r *runMetricsCounter) ErrShortCircuit(now time.Time) { r.shortCircuit++ }
114+
115+
var _ circuit.RunMetrics = (*runMetricsCounter)(nil)

internal/circuitgentest/circuittest_multi/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)