Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
35013a28
Commit
35013a28
authored
Jan 16, 2019
by
sharkpc0813
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add timeout test for alert handling.
parent
e172bade
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
127 additions
and
0 deletions
+127
-0
pkg/services/alerting/engine_test.go
+127
-0
No files found.
pkg/services/alerting/engine_test.go
View file @
35013a28
...
...
@@ -4,7 +4,11 @@ import (
"context"
"errors"
"math"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
.
"github.com/smartystreets/goconvey/convey"
)
...
...
@@ -34,6 +38,106 @@ func (handler *FakeResultHandler) Handle(evalContext *EvalContext) error {
return
nil
}
type
FakeCommonTimeoutHandler
struct
{
TransportTimeoutDuration
time
.
Duration
ServerBusySleepDuration
time
.
Duration
EvalSucceed
bool
ResultHandleSucceed
bool
}
func
NewFakeCommonTimeoutHandler
(
transportTimeoutDuration
time
.
Duration
,
serverBusySleepDuration
time
.
Duration
)
*
FakeCommonTimeoutHandler
{
return
&
FakeCommonTimeoutHandler
{
TransportTimeoutDuration
:
transportTimeoutDuration
,
ServerBusySleepDuration
:
serverBusySleepDuration
,
EvalSucceed
:
false
,
ResultHandleSucceed
:
false
,
}
}
func
(
handler
*
FakeCommonTimeoutHandler
)
Eval
(
evalContext
*
EvalContext
)
{
// 1. prepare mock server
path
:=
"/evaltimeout"
srv
:=
runBusyServer
(
path
,
handler
.
ServerBusySleepDuration
)
defer
srv
.
Close
()
// 2. send requests
url
:=
srv
.
URL
+
path
res
,
err
:=
sendRequest
(
evalContext
.
Ctx
,
url
,
handler
.
TransportTimeoutDuration
)
if
res
!=
nil
{
defer
res
.
Body
.
Close
()
}
if
err
!=
nil
{
evalContext
.
Error
=
errors
.
New
(
"Fake evaluation timeout test failure"
)
return
}
if
res
.
StatusCode
==
200
{
handler
.
EvalSucceed
=
true
}
evalContext
.
Error
=
errors
.
New
(
"Fake evaluation timeout test failure; wrong response"
)
}
func
(
handler
*
FakeCommonTimeoutHandler
)
Handle
(
evalContext
*
EvalContext
)
error
{
// 1. prepare mock server
path
:=
"/resulthandle"
srv
:=
runBusyServer
(
path
,
handler
.
ServerBusySleepDuration
)
defer
srv
.
Close
()
// 2. send requests
url
:=
srv
.
URL
+
path
res
,
err
:=
sendRequest
(
evalContext
.
Ctx
,
url
,
handler
.
TransportTimeoutDuration
)
if
res
!=
nil
{
defer
res
.
Body
.
Close
()
}
if
err
!=
nil
{
evalContext
.
Error
=
errors
.
New
(
"Fake result handle timeout test failure"
)
return
evalContext
.
Error
}
if
res
.
StatusCode
==
200
{
handler
.
ResultHandleSucceed
=
true
return
nil
}
evalContext
.
Error
=
errors
.
New
(
"Fake result handle timeout test failure; wrong response"
)
return
evalContext
.
Error
}
func
runBusyServer
(
path
string
,
serverBusySleepDuration
time
.
Duration
)
*
httptest
.
Server
{
mux
:=
http
.
NewServeMux
()
server
:=
httptest
.
NewServer
(
mux
)
mux
.
HandleFunc
(
path
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
time
.
Sleep
(
serverBusySleepDuration
)
})
return
server
}
func
sendRequest
(
context
context
.
Context
,
url
string
,
transportTimeoutInterval
time
.
Duration
)
(
resp
*
http
.
Response
,
err
error
)
{
req
,
err
:=
http
.
NewRequest
(
"GET"
,
url
,
nil
)
if
err
!=
nil
{
return
nil
,
err
}
req
=
req
.
WithContext
(
context
)
transport
:=
http
.
Transport
{
Dial
:
(
&
net
.
Dialer
{
Timeout
:
transportTimeoutInterval
,
KeepAlive
:
transportTimeoutInterval
,
})
.
Dial
,
}
client
:=
http
.
Client
{
Transport
:
&
transport
,
}
return
client
.
Do
(
req
)
}
func
TestEngineProcessJob
(
t
*
testing
.
T
)
{
Convey
(
"Alerting engine job processing"
,
t
,
func
()
{
engine
:=
NewEngine
()
...
...
@@ -113,6 +217,29 @@ func TestEngineProcessJob(t *testing.T) {
engine
.
processJobWithRetry
(
context
.
TODO
(),
job
)
So
(
evalHandler
.
CallNb
,
ShouldEqual
,
expectedAttempts
)
})
Convey
(
"pended alert for datasource -> result handler should be worked"
,
func
()
{
// reduce alert timeout to test quickly
originAlertTimeout
:=
alertTimeout
alertTimeout
=
5
*
time
.
Second
transportTimeoutInterval
:=
5
*
time
.
Second
serverBusySleepDuration
:=
4
*
time
.
Second
evalHandler
:=
NewFakeCommonTimeoutHandler
(
transportTimeoutInterval
,
serverBusySleepDuration
)
resultHandler
:=
NewFakeCommonTimeoutHandler
(
transportTimeoutInterval
,
serverBusySleepDuration
)
engine
.
evalHandler
=
evalHandler
engine
.
resultHandler
=
resultHandler
engine
.
processJobWithRetry
(
context
.
TODO
(),
job
)
So
(
evalHandler
.
EvalSucceed
,
ShouldEqual
,
true
)
So
(
resultHandler
.
ResultHandleSucceed
,
ShouldEqual
,
true
)
// initialize for other tests.
alertTimeout
=
originAlertTimeout
engine
.
resultHandler
=
&
FakeResultHandler
{}
})
})
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment