Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
138f1bbe
Commit
138f1bbe
authored
2 years ago
by
Josh Brooks
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for nickname
parent
d32d915b
No related branches found
No related tags found
4 merge requests
!510
Release
,
!419
rewrote the health tracker to both consider if there are waiting rounds and...
,
!397
Fully Decentralized channels
,
!340
Project/channels
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
channels/nickname.go
+40
-7
40 additions, 7 deletions
channels/nickname.go
channels/nickname_test.go
+79
-0
79 additions, 0 deletions
channels/nickname_test.go
with
119 additions
and
7 deletions
channels/nickname.go
+
40
−
7
View file @
138f1bbe
...
...
@@ -35,12 +35,13 @@ func loadOrNewNicknameManager(kv *versioned.KV) *nicknameManager {
jww
.
FATAL
.
Panicf
(
"Failed to load nicknameManager: %+v"
,
err
)
}
return
nm
,
nil
return
nm
}
// GetNickname returns the nickname for the given channel if it exists
func
(
nm
*
nicknameManager
)
GetNickname
(
ch
*
id
.
ID
)
(
nickname
string
,
exists
bool
)
{
func
(
nm
*
nicknameManager
)
GetNickname
(
ch
*
id
.
ID
)
(
nickname
string
,
exists
bool
)
{
nm
.
mux
.
RLock
()
defer
nm
.
mux
.
RUnlock
()
...
...
@@ -59,21 +60,39 @@ func (nm *nicknameManager) SetNickname(newNick string, ch *id.ID) error {
}
nm
.
byChannel
[
*
ch
]
=
newNick
return
n
il
return
n
m
.
save
()
}
// DeleteNickname removes the nickname for a given channel, using the codename
// for that channel instead
func
(
nm
*
nicknameManager
)
DeleteNickname
(
ch
*
id
.
ID
)
{
func
(
nm
*
nicknameManager
)
DeleteNickname
(
ch
*
id
.
ID
)
error
{
nm
.
mux
.
Lock
()
defer
nm
.
mux
.
Unlock
()
delete
(
nm
.
byChannel
,
*
ch
)
return
nm
.
save
()
}
// channelIDToNickname is a serialization structure. This is used by the save
// and load functions to serialize the nicknameManager's byChannel map.
type
channelIDToNickname
struct
{
channelId
id
.
ID
nickname
string
}
// save stores the nickname manager to disk. It must occur under the mux.
// save stores the nickname manager to disk. The caller of this must
// hold the mux.
func
(
nm
*
nicknameManager
)
save
()
error
{
data
,
err
:=
json
.
Marshal
(
&
nm
.
byChannel
)
list
:=
make
([]
channelIDToNickname
,
0
)
for
chId
,
nickname
:=
range
nm
.
byChannel
{
list
=
append
(
list
,
channelIDToNickname
{
channelId
:
chId
,
nickname
:
nickname
,
})
}
data
,
err
:=
json
.
Marshal
(
list
)
if
err
!=
nil
{
return
err
}
...
...
@@ -92,7 +111,19 @@ func (nm *nicknameManager) load() error {
if
err
!=
nil
{
return
err
}
return
json
.
Unmarshal
(
obj
.
Data
,
&
nm
.
byChannel
)
list
:=
make
([]
channelIDToNickname
,
0
)
err
=
json
.
Unmarshal
(
obj
.
Data
,
&
list
)
if
err
!=
nil
{
return
err
}
for
i
:=
range
list
{
current
:=
list
[
i
]
nm
.
byChannel
[
current
.
channelId
]
=
current
.
nickname
}
return
nil
}
// IsNicknameValid checks if a nickname is valid
...
...
@@ -104,4 +135,6 @@ func IsNicknameValid(nm string) error {
if
len
([]
rune
(
nm
))
>
24
{
return
errors
.
New
(
"nicknames must be 24 characters in length or less"
)
}
return
nil
}
This diff is collapsed.
Click to expand it.
channels/nickname_test.go
0 → 100644
+
79
−
0
View file @
138f1bbe
package
channels
import
(
"gitlab.com/elixxir/client/storage/versioned"
"gitlab.com/elixxir/ekv"
"gitlab.com/xx_network/primitives/id"
"strconv"
"testing"
)
// Unit test. Tests that once you set a nickname with SetNickname, you can
// retrieve the nickname using GetNickname.
func
TestNicknameManager_SetGetNickname
(
t
*
testing
.
T
)
{
kv
:=
versioned
.
NewKV
(
ekv
.
MakeMemstore
())
nm
:=
loadOrNewNicknameManager
(
kv
)
for
i
:=
0
;
i
<
numTests
;
i
++
{
chId
:=
id
.
NewIdFromUInt
(
uint64
(
i
),
id
.
User
,
t
)
nickname
:=
"nickname#"
+
strconv
.
Itoa
(
i
)
err
:=
nm
.
SetNickname
(
nickname
,
chId
)
if
err
!=
nil
{
t
.
Fatalf
(
"SetNickname error when setting %s: %+v"
,
nickname
,
err
)
}
received
,
_
:=
nm
.
GetNickname
(
chId
)
if
received
!=
nickname
{
t
.
Fatalf
(
"GetNickname did not return expected values."
+
"
\n
Expected: %s"
+
"
\n
Received: %s"
,
nickname
,
received
)
}
}
}
// Error case: Tests that nicknameManager.GetNickname returns a false boolean
// if no nickname has been set with the channel ID.
func
TestNicknameManager_GetNickname_Error
(
t
*
testing
.
T
)
{
kv
:=
versioned
.
NewKV
(
ekv
.
MakeMemstore
())
nm
:=
loadOrNewNicknameManager
(
kv
)
for
i
:=
0
;
i
<
numTests
;
i
++
{
chId
:=
id
.
NewIdFromUInt
(
uint64
(
i
),
id
.
User
,
t
)
_
,
exists
:=
nm
.
GetNickname
(
chId
)
if
exists
{
t
.
Fatalf
(
"GetNickname expected error case: "
+
"This should not retrieve nicknames for channel IDs "
+
"that are not set."
)
}
}
}
// Unit test. Check that once you SetNickname and DeleteNickname,
// GetNickname returns a false boolean.
func
TestNicknameManager_DeleteNickname
(
t
*
testing
.
T
)
{
kv
:=
versioned
.
NewKV
(
ekv
.
MakeMemstore
())
nm
:=
loadOrNewNicknameManager
(
kv
)
for
i
:=
0
;
i
<
numTests
;
i
++
{
chId
:=
id
.
NewIdFromUInt
(
uint64
(
i
),
id
.
User
,
t
)
nickname
:=
"nickname#"
+
strconv
.
Itoa
(
i
)
err
:=
nm
.
SetNickname
(
nickname
,
chId
)
if
err
!=
nil
{
t
.
Fatalf
(
"SetNickname error when setting %s: %+v"
,
nickname
,
err
)
}
err
=
nm
.
DeleteNickname
(
chId
)
if
err
!=
nil
{
t
.
Fatalf
(
"DeleteNickname error: %+v"
,
err
)
}
_
,
exists
:=
nm
.
GetNickname
(
chId
)
if
exists
{
t
.
Fatalf
(
"GetNickname expected error case: "
+
"This should not retrieve nicknames for channel IDs "
+
"that are not set."
)
}
}
}
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