go.dedis.ch/onet/v4@v4.0.0-pre1/network/big_test.go (about) 1 package network 2 3 import ( 4 "strconv" 5 "sync" 6 "testing" 7 8 "go.dedis.ch/onet/v4/log" 9 ) 10 11 /* 12 On MacOSX, for maximum number of connections, use 13 http://b.oldhu.com/2012/07/19/increase-tcp-max-connections-on-mac-os-x/ 14 sudo sysctl -w kern.maxfiles=12288 15 sudo sysctl -w kern.maxfilesperproc=10240 16 ulimit -n 10240 17 sudo sysctl -w kern.ipc.somaxconn=2048 18 */ 19 20 // There seems to be an error if a lot of hosts communicate with each other 21 // - this function tries to trigger that error so that it can be removed 22 // It generates one connection between each host and then starts sending 23 // messages all around. 24 func TestTCPHugeConnections(t *testing.T) { 25 // How many hosts are run 26 if testing.Short() { 27 t.Skip("Long test - skipping in short mode") 28 } 29 // How many hosts are run - if you try with nbrHosts >= 15, increase 30 // the maximum number of connections using the above snippet. 31 nbrHosts := 10 32 // 1MB of message size 33 msgSize := int64(1024 * 1024 * 1) 34 big := bigMessage{ 35 Msize: msgSize, 36 Msg: make([]byte, msgSize), 37 Pcrc: 25, 38 } 39 bigMessageType := RegisterMessage(big) 40 41 ids := make([]*ServerIdentity, nbrHosts) 42 hosts := make([]*TCPListener, nbrHosts) 43 // 2-dimensional array of connections between all hosts, where only 44 // the upper-right half is populated. The lower-left half is the 45 // mirror of the upper-right half, and the diagonal is empty, as there 46 // are no connections from one host to itself. 47 conns := make([][]Conn, nbrHosts) 48 wg := sync.WaitGroup{} 49 var err error 50 // Create all hosts and open the connections 51 for i := 0; i < nbrHosts; i++ { 52 addr := NewTCPAddress("localhost:" + strconv.Itoa(2000+i)) 53 ids[i] = NewTestServerIdentity(addr) 54 hosts[i], err = NewTCPListener(addr, tSuite) 55 if err != nil { 56 t.Fatal("Error setting up host:", err) 57 } 58 log.Lvl5("Host is", hosts[i], "id is", ids[i]) 59 go func(h int) { 60 err := hosts[h].Listen(func(c Conn) { 61 log.Lvl5(2000+h, "got a connection") 62 nm, err := c.Receive() 63 if err != nil { 64 t.Fatal("Couldn't receive msg:", err) 65 } 66 if !nm.MsgType.Equal(bigMessageType) { 67 t.Fatal("Received message type is wrong") 68 } 69 bigCopy := nm.Msg.(*bigMessage) 70 if bigCopy.Msize != msgSize { 71 t.Fatal(h, "Message-size is wrong:", bigCopy.Msize, bigCopy, big) 72 } 73 if bigCopy.Pcrc != 25 { 74 t.Fatal("CRC is wrong") 75 } 76 // And send it back 77 log.Lvl3(h, "sends it back") 78 79 go func(h int) { 80 log.Lvl3(h, "Sending back") 81 sentLen, err := c.Send(&big) 82 if err != nil { 83 t.Fatal(h, "couldn't send message:", err) 84 } 85 if sentLen == 0 { 86 t.Fatal("sentLen is zero") 87 } 88 }(h) 89 log.Lvl3(h, "done sending messages") 90 }) 91 if err != nil { 92 t.Fatal("Couldn't receive msg:", err) 93 } 94 }(i) 95 conns[i] = make([]Conn, nbrHosts) 96 for j := 0; j < i; j++ { 97 wg.Add(1) 98 var err error 99 log.Lvl5("Connecting", ids[i], "with", ids[j]) 100 conns[i][j], err = NewTCPConn(ids[j].Address, tSuite) 101 if err != nil { 102 t.Fatal("Couldn't open:", err) 103 } 104 // Populate also the lower left for easy sending to 105 // everybody 106 conns[j][i] = conns[i][j] 107 } 108 } 109 110 // Start sending messages back and forth 111 for i := 0; i < nbrHosts; i++ { 112 for j := 0; j < i; j++ { 113 c := conns[i][j] 114 go func(conn Conn, i, j int) { 115 defer wg.Done() 116 log.Lvl3("Sending from", i, "to", j, ":") 117 sentLen, err := conn.Send(&big) 118 if err != nil { 119 t.Fatal(i, j, "Couldn't send:", err) 120 } 121 if sentLen == 0 { 122 t.Fatal("sentLen is zero") 123 } 124 nm, err := conn.Receive() 125 if err != nil { 126 t.Fatal(i, j, "Couldn't receive:", err) 127 } 128 bc := nm.Msg.(*bigMessage) 129 if bc.Msize != msgSize { 130 t.Fatal(i, j, "Message-size is wrong") 131 } 132 if bc.Pcrc != 25 { 133 t.Fatal(i, j, "CRC is wrong") 134 } 135 log.Lvl3(i, j, "Done") 136 }(c, i, j) 137 } 138 } 139 wg.Wait() 140 141 // Close all 142 for _, h := range hosts { 143 if err := h.Stop(); err != nil { 144 t.Fatal("Couldn't close:", err) 145 } 146 } 147 } 148 149 type bigMessage struct { 150 Msize int64 151 Msg []byte 152 Pcrc int64 153 }