A random "instance ID" is generated on each start of the local discovery service. The instance ID is included in the announcement. When we see a new instance ID we treat is a new device and respond with an announcement of our own. Hence devices get to know each other quickly on restart. GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3385
76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
package discover
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
)
|
|
|
|
func TestRandomLocalInstanceID(t *testing.T) {
|
|
c, err := NewLocal(protocol.LocalDeviceID, ":0", &fakeAddressLister{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
go c.Serve()
|
|
defer c.Stop()
|
|
|
|
lc := c.(*localClient)
|
|
|
|
p0 := lc.announcementPkt()
|
|
p1 := lc.announcementPkt()
|
|
if p0.InstanceID == p1.InstanceID {
|
|
t.Error("each generated packet should have a new instance id")
|
|
}
|
|
}
|
|
|
|
func TestLocalInstanceIDShouldTriggerNew(t *testing.T) {
|
|
c, err := NewLocal(protocol.LocalDeviceID, ":0", &fakeAddressLister{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
lc := c.(*localClient)
|
|
src := &net.UDPAddr{IP: []byte{10, 20, 30, 40}, Port: 50}
|
|
|
|
new := lc.registerDevice(src, Announce{
|
|
ID: []byte{10, 20, 30, 40, 50, 60, 70, 80, 90},
|
|
Addresses: []string{"tcp://0.0.0.0:22000"},
|
|
InstanceID: 1234567890,
|
|
})
|
|
|
|
if !new {
|
|
t.Fatal("first register should be new")
|
|
}
|
|
|
|
new = lc.registerDevice(src, Announce{
|
|
ID: []byte{10, 20, 30, 40, 50, 60, 70, 80, 90},
|
|
Addresses: []string{"tcp://0.0.0.0:22000"},
|
|
InstanceID: 1234567890,
|
|
})
|
|
|
|
if new {
|
|
t.Fatal("second register should not be new")
|
|
}
|
|
|
|
new = lc.registerDevice(src, Announce{
|
|
ID: []byte{42, 10, 20, 30, 40, 50, 60, 70, 80, 90},
|
|
Addresses: []string{"tcp://0.0.0.0:22000"},
|
|
InstanceID: 1234567890,
|
|
})
|
|
|
|
if !new {
|
|
t.Fatal("new device ID should be new")
|
|
}
|
|
|
|
new = lc.registerDevice(src, Announce{
|
|
ID: []byte{10, 20, 30, 40, 50, 60, 70, 80, 90},
|
|
Addresses: []string{"tcp://0.0.0.0:22000"},
|
|
InstanceID: 91234567890,
|
|
})
|
|
|
|
if !new {
|
|
t.Fatal("new instance ID should be new")
|
|
}
|
|
}
|