Fixed the race. Made wait channel boolean.
This commit is contained in:
		| @@ -138,8 +138,8 @@ func (t *tun) monitor() { | |||||||
| 			return | 			return | ||||||
| 		case <-reconnect.C: | 		case <-reconnect.C: | ||||||
| 			for _, node := range t.options.Nodes { | 			for _, node := range t.options.Nodes { | ||||||
| 				if _, ok := t.links[node]; !ok { |  | ||||||
| 				t.Lock() | 				t.Lock() | ||||||
|  | 				if _, ok := t.links[node]; !ok { | ||||||
| 					link, err := t.setupLink(node) | 					link, err := t.setupLink(node) | ||||||
| 					if err != nil { | 					if err != nil { | ||||||
| 						log.Debugf("Tunnel failed to setup node link to %s: %v", node, err) | 						log.Debugf("Tunnel failed to setup node link to %s: %v", node, err) | ||||||
| @@ -147,8 +147,8 @@ func (t *tun) monitor() { | |||||||
| 						continue | 						continue | ||||||
| 					} | 					} | ||||||
| 					t.links[node] = link | 					t.links[node] = link | ||||||
| 					t.Unlock() |  | ||||||
| 				} | 				} | ||||||
|  | 				t.Unlock() | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -9,7 +9,7 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| // testAccept will accept connections on the transport, create a new link and tunnel on top | // testAccept will accept connections on the transport, create a new link and tunnel on top | ||||||
| func testAccept(t *testing.T, tun Tunnel, wait chan struct{}, wg *sync.WaitGroup) { | func testAccept(t *testing.T, tun Tunnel, wait chan bool, wg *sync.WaitGroup) { | ||||||
| 	// listen on some virtual address | 	// listen on some virtual address | ||||||
| 	tl, err := tun.Listen("test-tunnel") | 	tl, err := tun.Listen("test-tunnel") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @@ -17,7 +17,7 @@ func testAccept(t *testing.T, tun Tunnel, wait chan struct{}, wg *sync.WaitGroup | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// receiver ready; notify sender | 	// receiver ready; notify sender | ||||||
| 	wait <- struct{}{} | 	wait <- true | ||||||
|  |  | ||||||
| 	// accept a connection | 	// accept a connection | ||||||
| 	c, err := tl.Accept() | 	c, err := tl.Accept() | ||||||
| @@ -49,7 +49,7 @@ func testAccept(t *testing.T, tun Tunnel, wait chan struct{}, wg *sync.WaitGroup | |||||||
| } | } | ||||||
|  |  | ||||||
| // testSend will create a new link to an address and then a tunnel on top | // testSend will create a new link to an address and then a tunnel on top | ||||||
| func testSend(t *testing.T, tun Tunnel, wait chan struct{}, wg *sync.WaitGroup) { | func testSend(t *testing.T, tun Tunnel, wait chan bool, wg *sync.WaitGroup) { | ||||||
| 	defer wg.Done() | 	defer wg.Done() | ||||||
|  |  | ||||||
| 	// wait for the listener to get ready | 	// wait for the listener to get ready | ||||||
| @@ -110,7 +110,7 @@ func TestTunnel(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| 	defer tunA.Close() | 	defer tunA.Close() | ||||||
|  |  | ||||||
| 	wait := make(chan struct{}) | 	wait := make(chan bool) | ||||||
|  |  | ||||||
| 	var wg sync.WaitGroup | 	var wg sync.WaitGroup | ||||||
|  |  | ||||||
| @@ -140,7 +140,7 @@ func TestLoopbackTunnel(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| 	defer tun.Close() | 	defer tun.Close() | ||||||
|  |  | ||||||
| 	wait := make(chan struct{}) | 	wait := make(chan bool) | ||||||
|  |  | ||||||
| 	var wg sync.WaitGroup | 	var wg sync.WaitGroup | ||||||
|  |  | ||||||
| @@ -156,7 +156,7 @@ func TestLoopbackTunnel(t *testing.T) { | |||||||
| 	wg.Wait() | 	wg.Wait() | ||||||
| } | } | ||||||
|  |  | ||||||
| func testBrokenTunAccept(t *testing.T, tun Tunnel, wait chan struct{}, wg *sync.WaitGroup) { | func testBrokenTunAccept(t *testing.T, tun Tunnel, wait chan bool, wg *sync.WaitGroup) { | ||||||
| 	defer wg.Done() | 	defer wg.Done() | ||||||
|  |  | ||||||
| 	// listen on some virtual address | 	// listen on some virtual address | ||||||
| @@ -166,7 +166,7 @@ func testBrokenTunAccept(t *testing.T, tun Tunnel, wait chan struct{}, wg *sync. | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// receiver ready; notify sender | 	// receiver ready; notify sender | ||||||
| 	wait <- struct{}{} | 	wait <- true | ||||||
|  |  | ||||||
| 	// accept a connection | 	// accept a connection | ||||||
| 	c, err := tl.Accept() | 	c, err := tl.Accept() | ||||||
| @@ -196,7 +196,7 @@ func testBrokenTunAccept(t *testing.T, tun Tunnel, wait chan struct{}, wg *sync. | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// receiver ready; notify sender | 	// receiver ready; notify sender | ||||||
| 	wait <- struct{}{} | 	wait <- true | ||||||
|  |  | ||||||
| 	// accept a connection | 	// accept a connection | ||||||
| 	c, err = tl.Accept() | 	c, err = tl.Accept() | ||||||
| @@ -214,7 +214,7 @@ func testBrokenTunAccept(t *testing.T, tun Tunnel, wait chan struct{}, wg *sync. | |||||||
| 	<-wait | 	<-wait | ||||||
| } | } | ||||||
|  |  | ||||||
| func testBrokenTunSend(t *testing.T, tun Tunnel, wait chan struct{}, wg *sync.WaitGroup) { | func testBrokenTunSend(t *testing.T, tun Tunnel, wait chan bool, wg *sync.WaitGroup) { | ||||||
| 	defer wg.Done() | 	defer wg.Done() | ||||||
|  |  | ||||||
| 	// wait for the listener to get ready | 	// wait for the listener to get ready | ||||||
| @@ -252,7 +252,7 @@ func testBrokenTunSend(t *testing.T, tun Tunnel, wait chan struct{}, wg *sync.Wa | |||||||
| 	// wait for the listener to receive the message | 	// wait for the listener to receive the message | ||||||
| 	// c.Send merely enqueues the message to the link send queue and returns | 	// c.Send merely enqueues the message to the link send queue and returns | ||||||
| 	// in order to verify it was received we wait for the listener to tell us | 	// in order to verify it was received we wait for the listener to tell us | ||||||
| 	wait <- struct{}{} | 	wait <- true | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestReconnectTunnel(t *testing.T) { | func TestReconnectTunnel(t *testing.T) { | ||||||
| @@ -283,7 +283,7 @@ func TestReconnectTunnel(t *testing.T) { | |||||||
| 		t.Fatal(err) | 		t.Fatal(err) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	wait := make(chan struct{}) | 	wait := make(chan bool) | ||||||
|  |  | ||||||
| 	var wg sync.WaitGroup | 	var wg sync.WaitGroup | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user