Debug GetRequestJobs function and add unit tests
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
69a07d77d5
commit
0466b1fe05
@ -246,9 +246,10 @@ type RequestsJob struct {
|
|||||||
func (id *Identity) GetRequestJobs() []*RequestsJob {
|
func (id *Identity) GetRequestJobs() []*RequestsJob {
|
||||||
var list []*RequestsJob
|
var list []*RequestsJob
|
||||||
srvs := map[string]*RequestsJob{}
|
srvs := map[string]*RequestsJob{}
|
||||||
// build a server map
|
// get all servers
|
||||||
servers, err := id.MessageServers.LoadAllServers()
|
servers, err := id.MessageServers.LoadAllServers()
|
||||||
if err != nil {
|
if err == nil {
|
||||||
|
// build a server map
|
||||||
for _, server := range servers {
|
for _, server := range servers {
|
||||||
var rj RequestsJob
|
var rj RequestsJob
|
||||||
rj.Server = server
|
rj.Server = server
|
||||||
@ -271,8 +272,10 @@ func (id *Identity) GetRequestJobs() []*RequestsJob {
|
|||||||
// todo random reorder
|
// todo random reorder
|
||||||
|
|
||||||
// build list
|
// build list
|
||||||
for _, value := range srvs {
|
for _, srv := range srvs {
|
||||||
list = append(list, value)
|
if len(srv.LookupKeys) > 0 {
|
||||||
|
list = append(list, srv)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list
|
return list
|
||||||
|
@ -3,6 +3,7 @@ package client
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"forge.redroom.link/yves/meowlib"
|
"forge.redroom.link/yves/meowlib"
|
||||||
@ -79,3 +80,52 @@ func TestHidePeer(t *testing.T) {
|
|||||||
os.Remove("test.id")
|
os.Remove("test.id")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test GetRequestJobs
|
||||||
|
func TestGetRequestJobs(t *testing.T) {
|
||||||
|
// Create a mock Identity object
|
||||||
|
id := &Identity{
|
||||||
|
Peers: []Peer{
|
||||||
|
{
|
||||||
|
MyPullServers: []string{"server1", "server2"},
|
||||||
|
MyLookupKp: meowlib.NewKeyPair(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
MyPullServers: []string{"server3", "server4"},
|
||||||
|
MyLookupKp: meowlib.NewKeyPair(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
unlockedHiddenPeers: []Peer{
|
||||||
|
{
|
||||||
|
MyPullServers: []string{"server5", "server6"},
|
||||||
|
MyLookupKp: meowlib.NewKeyPair(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
id.MessageServers = ServerStorage{
|
||||||
|
DbFile: "test.db",
|
||||||
|
}
|
||||||
|
GetConfig().SetMemPass("test")
|
||||||
|
GetConfig().SetIdentity(id)
|
||||||
|
for i := 1; i < 10; i++ {
|
||||||
|
// initialize a Server with name "server+i"
|
||||||
|
srv := CreateServerFromUrl("server" + strconv.Itoa(i))
|
||||||
|
id.MessageServers.StoreServer(srv)
|
||||||
|
}
|
||||||
|
// Call GetRequestJobs
|
||||||
|
jobs := id.GetRequestJobs()
|
||||||
|
|
||||||
|
// Check that the returned list is as expected
|
||||||
|
assert.Equal(t, 6, len(jobs), "Expected 6 jobs")
|
||||||
|
|
||||||
|
// Check that each job has the correct server and lookup keys
|
||||||
|
for _, job := range jobs {
|
||||||
|
//fmt.Println(job.Server.GetUid(), job.LookupKeys)
|
||||||
|
assert.Contains(t, []string{"server1", "server2", "server3", "server4", "server5", "server6"}, job.Server.GetUid(), "Unexpected server UID")
|
||||||
|
assert.Len(t, job.LookupKeys, 1, "Expected 1 lookup key per job")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
// recursively remove the test.db folder
|
||||||
|
os.RemoveAll("test.db")
|
||||||
|
}
|
||||||
|
@ -2,6 +2,7 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"forge.redroom.link/yves/meowlib"
|
"forge.redroom.link/yves/meowlib"
|
||||||
@ -42,7 +43,9 @@ func TestStoreServer(t *testing.T) {
|
|||||||
if sout.Name != srv.Name {
|
if sout.Name != srv.Name {
|
||||||
log.Fatal("name not found")
|
log.Fatal("name not found")
|
||||||
}
|
}
|
||||||
|
// Clean up
|
||||||
|
// recursively remove the test.db folder
|
||||||
|
os.RemoveAll("test.db")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadServersFromUids(t *testing.T) {
|
func TestLoadServersFromUids(t *testing.T) {
|
||||||
@ -67,6 +70,9 @@ func TestLoadServersFromUids(t *testing.T) {
|
|||||||
if sout[0].Name != srv.Name {
|
if sout[0].Name != srv.Name {
|
||||||
log.Fatal("name not found")
|
log.Fatal("name not found")
|
||||||
}
|
}
|
||||||
|
// Clean up
|
||||||
|
// recursively remove the test.db folder
|
||||||
|
os.RemoveAll("test.db")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadServerCardsFromUids(t *testing.T) {
|
func TestLoadServerCardsFromUids(t *testing.T) {
|
||||||
@ -91,4 +97,7 @@ func TestLoadServerCardsFromUids(t *testing.T) {
|
|||||||
if sout[0].Name != srv.Name {
|
if sout[0].Name != srv.Name {
|
||||||
log.Fatal("name not found")
|
log.Fatal("name not found")
|
||||||
}
|
}
|
||||||
|
// Clean up
|
||||||
|
// recursively remove the test.db folder
|
||||||
|
os.RemoveAll("test.db")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user