Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
goatherd
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
arw
goatherd
Commits
6c451e74
Commit
6c451e74
authored
8 years ago
by
Lukas Braun
Browse files
Options
Downloads
Patches
Plain Diff
more tests for handle_conn
parent
0bbf0bf4
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
goatherd_test.go
+107
-25
107 additions, 25 deletions
goatherd_test.go
with
107 additions
and
25 deletions
goatherd_test.go
+
107
−
25
View file @
6c451e74
...
@@ -132,38 +132,55 @@ func check_offer_t(t *testing.T) {
...
@@ -132,38 +132,55 @@ func check_offer_t(t *testing.T) {
}
}
type
client_t
func
(
r
*
io
.
PipeReader
,
w
*
io
.
PipeWriter
)
func
mock_conn
(
name
string
,
offer
string
)
(
delay
*
sync
.
Mutex
,
answer
string
)
{
func
mock_conn
(
client
client_t
)
(
delay
*
sync
.
Mutex
)
{
in_r
,
in
_w
:=
io
.
Pipe
()
server_r
,
client
_w
:=
io
.
Pipe
()
in
_r_buf
:=
bufio
.
NewReader
(
in
_r
)
server
_r_buf
:=
bufio
.
NewReader
(
server
_r
)
defer
in
_r
.
Close
()
defer
server
_r
.
Close
()
out_r
,
out
_w
:=
io
.
Pipe
()
client_r
,
server
_w
:=
io
.
Pipe
()
out_r_buf
,
out
_w_buf
:=
bufio
.
New
Reader
(
out_r
),
bufio
.
NewWriter
(
out
_w
)
server
_w_buf
:=
bufio
.
New
Writer
(
server
_w
)
defer
ou
t_r
.
Close
()
defer
clien
t_r
.
Close
()
answer_c
:=
make
(
chan
string
)
go
client
(
client_r
,
client_w
)
go
func
()
{
delay
=
handle_conn
(
db
,
"handle_conn_t"
,
server_r_buf
,
server_w_buf
)
in_w
.
Write
(
append
([]
byte
(
username
),
'\n'
))
in_w
.
Write
(
append
([]
byte
(
offer
),
'\n'
))
answer
,
_
:=
get_line
(
out_r_buf
)
answer_c
<-
answer
}()
delay
=
handle_conn
(
db
,
"handle_conn_t"
,
in_r_buf
,
out_w_buf
)
answer
=
<-
answer_c
return
return
}
}
func
interact
(
name
string
,
offer
string
,
recv
chan
string
)
client_t
{
return
func
(
r
*
io
.
PipeReader
,
w
*
io
.
PipeWriter
)
{
w
.
Write
(
append
([]
byte
(
name
),
'\n'
))
w
.
Write
(
append
([]
byte
(
offer
),
'\n'
))
answer
,
_
:=
get_line
(
bufio
.
NewReader
(
r
))
recv
<-
answer
}
}
func
handle_conn_t
(
t
*
testing
.
T
)
{
func
handle_conn_t
(
t
*
testing
.
T
)
{
// zero value Mutex is unlocked, can be compared to a given Mutex to
// zero value Mutex is unlocked, can be compared to a given Mutex to
// (unreliably?) check if it is locked
// (unreliably?) check if it is locked
unlocked
:=
sync
.
Mutex
{}
unlocked
:=
sync
.
Mutex
{}
t
.
Run
(
"dummy_user"
,
func
(
t
*
testing
.
T
)
{
recv
:=
make
(
chan
string
)
delay
,
answer
:=
mock_conn
(
interact
(
"dummy user"
,
"dummy offer"
,
recv
)),
<-
recv
if
delay
!=
nil
{
t
.
Fail
()
}
if
answer
!=
""
{
t
.
Error
(
"answer:"
,
answer
)
}
if
faildelay
.
userlocks
[
"dummy user"
]
!=
nil
{
t
.
Error
(
"userlock created for dummy user"
)
}
})
t
.
Run
(
"dummy_offer"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"dummy_offer"
,
func
(
t
*
testing
.
T
)
{
delay
,
answer
:=
mock_conn
(
username
,
"dummy offer"
)
recv
:=
make
(
chan
string
)
delay
,
answer
:=
mock_conn
(
interact
(
username
,
"dummy offer"
,
recv
)),
<-
recv
if
delay
==
nil
{
if
delay
==
nil
{
t
.
Fail
()
t
.
Fail
()
}
else
if
*
delay
==
unlocked
{
}
else
if
*
delay
==
unlocked
{
...
@@ -174,16 +191,21 @@ func handle_conn_t(t *testing.T) {
...
@@ -174,16 +191,21 @@ func handle_conn_t(t *testing.T) {
if
answer
!=
"FAIL"
{
if
answer
!=
"FAIL"
{
t
.
Error
(
"answer:"
,
answer
)
t
.
Error
(
"answer:"
,
answer
)
}
}
if
faildelay
.
userlocks
[
username
]
==
nil
{
t
.
Error
(
"userlock not in map"
)
}
})
})
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
tx
,
err
:=
db
.
Begin
()
tx
,
err
:=
db
.
Begin
()
t_err_fatal
(
t
,
err
)
t_err_fatal
(
t
,
err
)
otp
,
err
:=
get_otp
(
tx
,
username
)
otp
,
err
:=
get_otp
(
tx
,
username
)
t_err_fatal
(
t
,
err
)
t_err_fatal
(
t
,
err
)
tx
.
Commit
()
tx
.
Commit
()
delay
,
answer
:=
mock_conn
(
username
,
otp
.
OTP
())
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
recv
:=
make
(
chan
string
)
delay
:=
mock_conn
(
interact
(
username
,
otp
.
OTP
(),
recv
))
answer
:=
<-
recv
if
delay
!=
nil
{
if
delay
!=
nil
{
t
.
Fail
()
t
.
Fail
()
}
}
...
@@ -194,6 +216,66 @@ func handle_conn_t(t *testing.T) {
...
@@ -194,6 +216,66 @@ func handle_conn_t(t *testing.T) {
t
.
Error
(
"answer:"
,
answer
)
t
.
Error
(
"answer:"
,
answer
)
}
}
})
})
otp
.
Increment
()
t
.
Run
(
"read_error"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"close_immediately"
,
func
(
t
*
testing
.
T
)
{
delay
:=
mock_conn
(
func
(
r
*
io
.
PipeReader
,
w
*
io
.
PipeWriter
)
{
w
.
Close
()
})
if
delay
!=
nil
{
t
.
Fail
()
}
if
*
faildelay
.
userlocks
[
username
]
!=
unlocked
{
t
.
Error
(
"userlock not unlocked"
)
}
})
t
.
Run
(
"close_after_username"
,
func
(
t
*
testing
.
T
)
{
delay
:=
mock_conn
(
func
(
r
*
io
.
PipeReader
,
w
*
io
.
PipeWriter
)
{
w
.
Write
(
append
([]
byte
(
username
),
'\n'
))
w
.
Close
()
})
if
delay
!=
nil
{
t
.
Fail
()
}
if
*
faildelay
.
userlocks
[
username
]
!=
unlocked
{
t
.
Error
(
"userlock not unlocked"
)
}
})
})
t
.
Run
(
"write_error"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"FAIL"
,
func
(
t
*
testing
.
T
)
{
delay
:=
mock_conn
(
func
(
r
*
io
.
PipeReader
,
w
*
io
.
PipeWriter
)
{
r
.
Close
()
w
.
Write
(
append
([]
byte
(
username
),
'\n'
))
w
.
Write
(
append
([]
byte
(
"dummy offer"
),
'\n'
))
})
if
delay
==
nil
{
t
.
Fail
()
}
else
if
*
delay
==
unlocked
{
t
.
Error
(
"delay mutex is unlocked"
)
}
else
{
delay
.
Unlock
()
}
})
t
.
Run
(
"OK"
,
func
(
t
*
testing
.
T
)
{
delay
:=
mock_conn
(
func
(
r
*
io
.
PipeReader
,
w
*
io
.
PipeWriter
)
{
r
.
Close
()
w
.
Write
(
append
([]
byte
(
username
),
'\n'
))
w
.
Write
(
append
([]
byte
(
otp
.
OTP
()),
'\n'
))
})
if
delay
!=
nil
{
t
.
Fail
()
}
if
*
faildelay
.
userlocks
[
username
]
!=
unlocked
{
t
.
Error
(
"userlock not unlocked"
)
}
})
otp
.
Increment
()
})
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment