fusen-render/start_test.go
2023-07-30 06:50:58 +08:00

179 lines
3.4 KiB
Go

package fusenrender_test
import (
"context"
"fusenrender"
"log"
"testing"
"time"
"github.com/lni/goutils/syncutil"
)
func init() {
log.SetFlags(log.Llongfile)
}
func TestStartNodeA(t *testing.T) {
svc, err := fusenrender.LoadConfig("etc/etc_a.yaml")
if err != nil {
panic(err)
}
item := &fusenrender.QueueItem{
Group: "test",
Priority: uint32(1),
CreateAt: time.Now(),
Data: "a",
}
a := fusenrender.StartNode(svc.ServerID, 128, svc.Address())
raftStopper := syncutil.NewStopper()
// ch := make(chan string, 16)
raftStopper.RunWorker(func() {
// this goroutine makes a linearizable read every 10 second. it returns the
// Count value maintained in IStateMachine. see datastore.go for details.
cs := a.GetNoOPSession(128)
ticker := time.NewTicker(5 * time.Second)
for {
select {
case <-ticker.C:
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
cmd := fusenrender.Command{
Name: "enqueue",
Group: "test",
Item: item,
}
data, err := cmd.Encode()
if err != nil {
log.Println(err)
}
result, err := a.SyncPropose(ctx, cs, data)
if err != nil {
log.Println(err)
}
log.Println(len(result.Data))
cancel()
case <-raftStopper.ShouldStop():
return
}
}
})
raftStopper.Wait()
}
func TestStartNodeB(t *testing.T) {
svc, err := fusenrender.LoadConfig("etc/etc_b.yaml")
if err != nil {
panic(err)
}
item := &fusenrender.QueueItem{
Group: "test",
Priority: uint32(1),
CreateAt: time.Now(),
Data: "b",
}
nh := fusenrender.StartNode(svc.ServerID, 128, svc.Address())
raftStopper := syncutil.NewStopper()
// ch := make(chan string, 16)
raftStopper.RunWorker(func() {
// this goroutine makes a linearizable read every 10 second. it returns the
// Count value maintained in IStateMachine. see datastore.go for details.
cs := nh.GetNoOPSession(128)
ticker := time.NewTicker(5 * time.Second)
for {
select {
case <-ticker.C:
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
cmd := fusenrender.Command{
Name: "enqueue",
Group: "test",
Item: item,
}
data, err := cmd.Encode()
if err != nil {
log.Println(err)
}
result, err := nh.SyncPropose(ctx, cs, data)
if err != nil {
log.Println(err)
}
log.Println(result)
cancel()
case <-raftStopper.ShouldStop():
return
}
}
})
raftStopper.Wait()
}
func TestStartNodeC(t *testing.T) {
svc, err := fusenrender.LoadConfig("etc/etc_c.yaml")
if err != nil {
panic(err)
}
nh := fusenrender.StartNode(svc.ServerID, 128, svc.Address())
raftStopper := syncutil.NewStopper()
// ch := make(chan string, 16)
raftStopper.RunWorker(func() {
// this goroutine makes a linearizable read every 10 second. it returns the
// Count value maintained in IStateMachine. see datastore.go for details.
cs := nh.GetNoOPSession(128)
ticker := time.NewTicker(1 * time.Second)
for {
select {
case <-ticker.C:
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
cmd := fusenrender.Command{
Name: "dequeue",
Group: "test",
}
data, err := cmd.Encode()
if err != nil {
log.Println(err)
}
result, err := nh.SyncPropose(ctx, cs, data)
if err != nil {
log.Println(err)
}
log.Println(string(result.Data))
cancel()
case <-raftStopper.ShouldStop():
return
}
}
})
raftStopper.Wait()
}