mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 15:04:03 +03:00
Add EventIPBlocklisted
This commit is contained in:
@@ -73,6 +73,8 @@ func eventStreamProcessor(ctx context.Context, eventChan <-chan mtglib.Event, ob
|
||||
observer.EventStart(typedEvt)
|
||||
case mtglib.EventFinish:
|
||||
observer.EventFinish(typedEvt)
|
||||
case mtglib.EventIPBlocklisted:
|
||||
observer.EventIPBlocklisted(typedEvt)
|
||||
case mtglib.EventConcurrencyLimited:
|
||||
observer.EventConcurrencyLimited(typedEvt)
|
||||
}
|
||||
|
||||
@@ -106,6 +106,29 @@ func (suite *EventStreamTestSuite) TestEventConcurrencyLimitedOk() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
|
||||
func (suite *EventStreamTestSuite) TestEventIPBlocklistedOk() {
|
||||
evt := mtglib.EventIPBlocklisted{
|
||||
CreatedAt: time.Now(),
|
||||
RemoteIP: net.ParseIP("10.0.0.10"),
|
||||
}
|
||||
|
||||
for _, v := range []*ObserverMock{suite.observerMock1, suite.observerMock2} {
|
||||
v.
|
||||
On("EventIPBlocklisted", mock.Anything).
|
||||
Once().
|
||||
Run(func(args mock.Arguments) {
|
||||
caught := args.Get(0).(mtglib.EventIPBlocklisted)
|
||||
|
||||
suite.Equal(evt.CreatedAt, caught.CreatedAt)
|
||||
suite.Equal(evt.StreamID(), caught.StreamID())
|
||||
suite.Equal(evt.RemoteIP.String(), caught.RemoteIP.String())
|
||||
})
|
||||
}
|
||||
|
||||
suite.stream.Send(suite.ctx, evt)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
|
||||
func (suite *EventStreamTestSuite) TearDownTest() {
|
||||
suite.stream.Shutdown()
|
||||
suite.ctxCancel()
|
||||
|
||||
@@ -6,6 +6,7 @@ type Observer interface {
|
||||
EventStart(mtglib.EventStart)
|
||||
EventFinish(mtglib.EventFinish)
|
||||
EventConcurrencyLimited(mtglib.EventConcurrencyLimited)
|
||||
EventIPBlocklisted(mtglib.EventIPBlocklisted)
|
||||
|
||||
Shutdown()
|
||||
}
|
||||
|
||||
@@ -21,6 +21,10 @@ func (o *ObserverMock) EventConcurrencyLimited(evt mtglib.EventConcurrencyLimite
|
||||
o.Called(evt)
|
||||
}
|
||||
|
||||
func (o *ObserverMock) EventIPBlocklisted(evt mtglib.EventIPBlocklisted) {
|
||||
o.Called(evt)
|
||||
}
|
||||
|
||||
func (o *ObserverMock) Shutdown() {
|
||||
o.Called()
|
||||
}
|
||||
|
||||
@@ -55,6 +55,21 @@ func (m multiObserver) EventConcurrencyLimited(evt mtglib.EventConcurrencyLimite
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (m multiObserver) EventIPBlocklisted(evt mtglib.EventIPBlocklisted) {
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(len(m.observers))
|
||||
|
||||
for _, v := range m.observers {
|
||||
go func(obs Observer) {
|
||||
defer wg.Done()
|
||||
|
||||
obs.EventIPBlocklisted(evt)
|
||||
}(v)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (m multiObserver) Shutdown() {
|
||||
for _, v := range m.observers {
|
||||
v.Shutdown()
|
||||
|
||||
@@ -20,6 +20,7 @@ type noopObserver struct{}
|
||||
func (n noopObserver) EventStart(_ mtglib.EventStart) {}
|
||||
func (n noopObserver) EventFinish(_ mtglib.EventFinish) {}
|
||||
func (n noopObserver) EventConcurrencyLimited(_ mtglib.EventConcurrencyLimited) {}
|
||||
func (n noopObserver) EventIPBlocklisted(_ mtglib.EventIPBlocklisted) {}
|
||||
func (n noopObserver) Shutdown() {}
|
||||
|
||||
func NewNoopObserver() Observer {
|
||||
|
||||
@@ -30,6 +30,10 @@ func (suite *NoopTestSuite) SetupSuite() {
|
||||
ConnID: "connID",
|
||||
},
|
||||
"concurrency-limited": mtglib.EventConcurrencyLimited{},
|
||||
"ip-blacklisted": mtglib.EventIPBlocklisted{
|
||||
RemoteIP: net.ParseIP("10.0.0.10"),
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
}
|
||||
suite.ctx = context.Background()
|
||||
}
|
||||
@@ -62,6 +66,8 @@ func (suite *NoopTestSuite) TestObserver() {
|
||||
observer.EventFinish(typedEvt)
|
||||
case mtglib.EventConcurrencyLimited:
|
||||
observer.EventConcurrencyLimited(typedEvt)
|
||||
case mtglib.EventIPBlocklisted:
|
||||
observer.EventIPBlocklisted(typedEvt)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user