You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.0 KiB

package server
import (
"encoding/hex"
"fmt"
"net"
"testing"
"time"
"../model"
)
/*
Send data to server
*/
func sendData(conf *model.ServerConfig, data []byte, dur chan<- int64) error {
//simulate client
stTime := time.Now()
addr := fmt.Sprintf("%s:%d", conf.IP, conf.Port)
conn, err := net.Dial("tcp", addr)
if err != nil {
fmt.Println("Error: ", err)
dur <- -1
return err
}
defer conn.Close()
nw, err := conn.Write(data)
if err != nil {
fmt.Printf("Write error: %+v\n", err)
dur <- -1
return err
}
fmt.Println("Packet writen = ", nw)
//sleep awhile
//time.Sleep(time.Second)
//read response
rep := make([]byte, 20)
nr, err := conn.Read(rep)
if err != nil {
fmt.Printf("Read error: %+v\n", err)
dur <- -1
return err
}
fmt.Println("Response -> ", rep[:nr])
dur <- time.Now().Sub(stTime).Nanoseconds()
return nil
}
func TestInvalid(t *testing.T) {
//config
conf := model.ServerConfig{IP: "127.0.0.1", Port: 8095}
//test failed data
dur := make(chan int64, 1)
data3 := []byte{0x08, 0x00, 0x01, 0x02}
sendData(&conf, data3, dur)
<-dur
}
//start sending data
func TestRuptelaReceiver(t *testing.T) {
//send data
data1, _ := hex.DecodeString(
"007900000b1a2a5585c30100024e9c036900000f101733208ff45e07b31b570a001009090605011b1a020003001c01ad01021d338e1600000" +
"2960000601a41014bc16d004e9c038400000f104fdf20900d20075103b00a001308090605011b1a020003001c01ad01021d33b11600000296" +
"0000601a41014bc1ea0028f9")
data2, _ := hex.DecodeString(
"033500000C076B5C208F01011E5268CEF20000196E3A3A0AEF3E934F3E2D780000000007000000005268CEFD0000196E3A3A0AEF3E934F3" +
"E2D780000000007000000005268CF080000196E3A3A0AEF3E934F3E2D780000000007000000005268CF130000196E3A3A0AEF3E934F3E2D7" +
"80000000007000000005268CF1E0000196E3A3A0AEF3E934F3E2D780000000007000000005268CF290000196E3A3A0AEF3E934F3E2D78000" +
"0000007000000005268CF340000196E3A3A0AEF3E934F3E2D780000000007000000005268CF3F0000196E3A3A0AEF3E934F3E2D780000000" +
"007000000005268CF4A0000196E3A3A0AEF3E934F3E2D780000000007000000005268CF550000196E3A3A0AEF3E934F3E2D7800000000070" +
"00000005268CF600000196E3A3A0AEF3E934F3E2D780000000007000000005268CF6B0000196E3A3A0AEF3E934F3E2D78000000000700000" +
"0005268CF730000196E36630AEF42CE4F6D0BF40400022208000000005268CF7E0000196E36B60AEF42BE4F6D0BF400000000070000000052" +
"68CF890000196E36B60AEF42BE4F6D0BF40000000007000000005268CF940000196E36B60AEF42BE4F6D0BF40000000007000000005268CF" +
"9F0000196E36B60AEF42BE4F6D0BF40000000007000000005268CFAA0000196E36B60AEF42BE4F6D0BF40000000007000000005268CFB50" +
"000196E36B60AEF42BE4F6D0BF40000000007000000005268CFC00000196E36B60AEF42BE4F6D0BF40000000007000000005268CFCB00001" +
"96E36B60AEF42BE4F6D0BF40000000007000000005268CFD60000196E36B60AEF42BE4F6D0BF40000000007000000005268CFD70000196E" +
"3C710AEF5EFF4F690BF40400011708000000005268CFE20000196E3B980AEF601A4F690BF40000000007000000005268CFED0000196E3B980" +
"AEF601A4F690BF40000000007000000005268CFF80000196E3B980AEF601A4F690BF40000000007000000005268D0030000196E3B980AEF60" +
"1A4F690BF40000000007000000005268D00E0000196E3B980AEF601A4F690BF40000000007000000005268D0190000196E3B980AEF601A4F6" +
"90BF40000000007000000005268D0240000196E3B980AEF601A4F690BF400000000070000000046E2")
data3, _ := hex.DecodeString(
"0011000315A07F44865A0E01000053EA01DF65AD6D")
const NTEST = 1
conf := model.ServerConfig{IP: "127.0.0.1", Port: 8081}
ch := make(chan int64, NTEST*3)
n := 0
for i := 0; i < NTEST; i++ {
go sendData(&conf, data1, ch)
n++
go sendData(&conf, data2, ch)
n++
go sendData(&conf, data3, ch)
n++
}
var ma int64 = 0
var mi int64 = 10000000000000
var tot float64 = 0
var ti int64
for i := 0; i < NTEST*2; i++ {
ti = <-ch
if ti < 0 {
continue
}
if ti < mi {
mi = ti
}
if ti > ma {
ma = ti
}
tot += float64(ti)
}
const MILIS = 1000000
avg := tot / (NTEST * 2)
fmt.Printf("Total time = %+v ms, avg = %+v ms, mi = %+v ms, ma = %+v ms\n",
tot/MILIS, avg/MILIS, float64(mi)/MILIS, float64(ma)/MILIS)
}