//For testing large messages..... func Test_Cluster_Message_Length(t *testing.T) { total := 5 var server [5]cluster.Server for i := 0; i < total; i++ { server[i] = cluster.New(i+1, "../config.json") } message := make([]byte, 1000000) x := &cluster.Envelope{SendTo: -1, SendBy: 1, Msg: message} //Broadcasting long data server[0].Outbox() <- x for i := 0; i < total; i++ { close(server[i].Outbox()) } b, _ := json.Marshal(*x) // Calculating if all servers recieve the data fully... for i := 1; i < total; i++ { envelope := <-server[i].Inbox() c, _ := json.Marshal(*envelope) if len(c) != len(b) { panic("Message not recieved fully..") } } t.Log("Length test passed.") }
// For testing cyclic data func Test_Cluster_Availability(t *testing.T) { total := 5 var server [5]cluster.Server // server[4] is not started now.. (corresponding to pid=5) for i := 0; i < total-1; i++ { server[i] = cluster.New(i+1, "../config.json") } // Random messages message := "hello" count := make([]int, 5) for i := 0; i < total; i++ { count[i] = 0 } for k := 0; k < total-1; k++ { server[k].Outbox() <- &cluster.Envelope{SendTo: -1, SendBy: k + 1, Msg: message} } time.Sleep(time.Second) server[total-1] = cluster.New(total, "../config.json") wg := new(sync.WaitGroup) for i := 0; i < total; i++ { wg.Add(1) go checkInput(server[i], &count[i], wg) } for i := 0; i < total; i++ { close(server[i].Outbox()) } wg.Wait() if count[4] != 4 { panic("All messages not recieved..") } t.Log("test of Availability of cluster passed.") }
func main() { // parse argument flags and get this server's id into myid mypid := flag.Int("pid", 0, "Pid of my own system") flag.Parse() server := cluster.New(*mypid /* config file */, "./config.json") // wait for keystroke to start. r := bufio.NewReader(os.Stdin) _, err := r.ReadString('\n') if err != nil { os.Exit(1) } // Let each server broadcast a message server.Outbox() <- &cluster.Envelope{SendTo: -1, SendBy: *mypid, Msg: "hello there"} close(server.Outbox()) var input string fmt.Scanln(&input) }
// For testing cyclic data func Test_Cluster_Cyclic_Dependencies(t *testing.T) { total := 5 var server [5]cluster.Server for i := 0; i < total; i++ { server[i] = cluster.New(i+1, "../config.json") } // Random messages message := "hello" count := make([]int, 5) for i := 0; i < total; i++ { count[i] = 0 } wg := new(sync.WaitGroup) for i := 0; i < total; i++ { wg.Add(1) go checkInput(server[i], &count[i], wg) } iterations := 1000 for j := 0; j < iterations; j++ { for k := 0; k < total; k++ { server[k].Outbox() <- &cluster.Envelope{SendTo: (k+1)%total + 1, SendBy: k + 1, Msg: message} } } for i := 0; i < total; i++ { close(server[i].Outbox()) } wg.Wait() for i := 0; i < total; i++ { if count[i] != iterations { panic("All messages not recieved..") } } t.Log("Cyclic message test passed.") }
//Load testing... Tested for more than 130000 messages func Test_Cluster_Load(t *testing.T) { total := 5 var server [5]cluster.Server for i := 0; i < total; i++ { server[i] = cluster.New(i+1, "../config.json") } // Random messages message := []string{"hello", "hi", "how are you", "hello its long day", "hoho"} //Populating pids array with all pids including -1 peers := server[0].Peers() n := len(peers) pids := make([]int, n+2) for i := 0; i < n; i++ { pids[i] = peers[i] } pids[n] = server[0].Pid() pids[n+1] = -1 N_msg := 0 count := make([]int, 5) for i := 0; i < total; i++ { count[i] = 0 } wg := new(sync.WaitGroup) for i := 0; i < total; i++ { wg.Add(1) go checkInput(server[i], &count[i], wg) } for j := 0; j < 100000; j++ { // Random sender x := rand.Intn(total) // Random reciever (pids[y]) y := rand.Intn(n + 2) // Random message z := rand.Intn(5) if pids[y] == (x + 1) { continue } // Calculating total messages to be sent ideally. if pids[y] == -1 { N_msg += 4 } else { N_msg++ } // Sending message to channel server[x].Outbox() <- &cluster.Envelope{SendTo: pids[y], SendBy: x + 1, Msg: message[z]} } for i := 0; i < total; i++ { close(server[i].Outbox()) } // Waiting for goroutines to end wg.Wait() var totalCount int for i := 0; i < total; i++ { totalCount += count[i] } if totalCount != N_msg { panic("All messages not recieved..") } t.Log("Load test passed.") }