func (s *PtreeSuite) TestInsertNodeSplit(c *gc.C) { root, err := s.ptree.Root() for _, sv := range root.SValues() { c.Log("SV:", sv) c.Assert(sv.Cmp(cf.Zi(cf.P_SKS, 1)), gc.Equals, 0) } // Add a bunch of nodes, enough to cause splits for i := 0; i < s.config.SplitThreshold()*4; i++ { z := cf.Zi(cf.P_SKS, i+65536) c.Log("Insert:", z) s.ptree.Insert(z) } // Remove a bunch of nodes, enough to cause joins for i := 0; i < s.config.SplitThreshold()*4; i++ { z := cf.Zi(cf.P_SKS, i+65536) c.Log("Remove:", z) s.ptree.Remove(z) } root, err = s.ptree.Root() c.Assert(err, gc.IsNil) // Insert/Remove reversible after splitting & joining? for _, sv := range root.SValues() { c.Log("SV:", sv) c.Assert(sv.Cmp(cf.Zi(cf.P_SKS, 1)), gc.Equals, 0) } c.Assert(recon.MustChildren(root), gc.HasLen, 0) c.Assert(recon.MustElements(root), gc.HasLen, 0) }
// TestKeyMatch tests key consistency func (s *PtreeSuite) TestKeyMatch(c *gc.C) { tree1 := new(MemPrefixTree) tree1.Init() for i := 1; i < 100; i++ { tree1.Insert(cf.Zi(cf.P_SKS, 65537*i+i)) } // Some extra samples for i := 1; i < 50; i++ { tree1.Insert(cf.Zi(cf.P_SKS, 68111*i)) } tree2 := new(MemPrefixTree) tree2.Init() for i := 1; i < 100; i++ { tree2.Insert(cf.Zi(cf.P_SKS, 65537*i)) } // One extra sample for i := 1; i < 20; i++ { tree2.Insert(cf.Zi(cf.P_SKS, 70001*i)) } for i := 1; i < 100; i++ { zi := cf.Zi(cf.P_SKS, 65537*i) bs := cf.NewZpBitstring(zi) node1, err := Find(tree1, zi) c.Assert(err, gc.IsNil) node2, err := Find(tree2, zi) c.Assert(err, gc.IsNil) c.Logf("node1=%v, node2=%v (%b) full=%v", node1.Key(), node2.Key(), zi.Int64(), bs) // If keys are different, one must prefix the other. c.Assert(strings.HasPrefix(node1.Key().String(), node2.Key().String()) || strings.HasPrefix(node2.Key().String(), node1.Key().String()), gc.Equals, true) } }
// Test full node sync. func (s *ReconSuite) TestFullSync(c *gc.C) { ptree1, cleanup, err := s.Factory() c.Assert(err, gc.IsNil) defer cleanup() ptree2, cleanup, err := s.Factory() c.Assert(err, gc.IsNil) defer cleanup() ptree1.Insert(cf.Zi(cf.P_SKS, 65537)) ptree1.Insert(cf.Zi(cf.P_SKS, 65539)) root, _ := ptree1.Root() c.Log("peer1:", recon.MustElements(root)) ptree2.Insert(cf.Zi(cf.P_SKS, 65537)) ptree2.Insert(cf.Zi(cf.P_SKS, 65541)) root, _ = ptree2.Root() c.Log("peer2:", recon.MustElements(root)) port1, port2 := portPair(c) peer1 := s.newPeer(port1, port2, recon.PeerModeGossipOnly, ptree1) peer2 := s.newPeer(port2, port1, recon.PeerModeServeOnly, ptree2) err = s.pollRootConvergence(c, peer1, peer2, ptree1, ptree2) c.Assert(err, gc.IsNil) }
func (t *prefixTree) newChildNode(parent *prefixNode, childIndex int) *prefixNode { n := &prefixNode{prefixTree: t, Leaf: true} var key *cf.Bitstring if parent != nil { parentKey := parent.Key() key = cf.NewBitstring(parentKey.BitLen() + t.BitQuantum) key.SetBytes(parentKey.Bytes()) for j := 0; j < parent.BitQuantum; j++ { if (1<<uint(j))&childIndex == 0 { key.Clear(parentKey.BitLen() + j) } else { key.Set(parentKey.BitLen() + j) } } } else { key = cf.NewBitstring(0) } n.NodeKey = mustEncodeBitstring(key) svalues := make([]*cf.Zp, t.NumSamples()) for i := 0; i < len(svalues); i++ { svalues[i] = cf.Zi(cf.P_SKS, 1) } n.NodeSValues = mustEncodeZZarray(svalues) return n }
func (s *ReconSuite) RunOneSided(c *gc.C, n int, serverHas bool, timeout time.Duration) { ptree1, cleanup, err := s.Factory() c.Assert(err, gc.IsNil) defer cleanup() ptree2, cleanup, err := s.Factory() c.Assert(err, gc.IsNil) defer cleanup() expected := cf.NewZSet() for i := 1; i < n; i++ { z := cf.Zi(cf.P_SKS, 65537*i) ptree2.Insert(z) expected.Add(z) } port1, port2 := portPair(c) var peer1Mode recon.PeerMode var peer2Mode recon.PeerMode if serverHas { peer1Mode = recon.PeerModeGossipOnly peer2Mode = recon.PeerModeServeOnly } else { peer1Mode = recon.PeerModeServeOnly peer2Mode = recon.PeerModeGossipOnly } peer1 := s.newPeer(port1, port2, peer1Mode, ptree1) peer2 := s.newPeer(port2, port1, peer2Mode, ptree2) err = s.pollConvergence(c, peer1, peer2, expected, cf.NewZSet(), timeout) c.Assert(err, gc.IsNil) }
func (n *MemPrefixNode) init(t *MemPrefixTree) { n.MemPrefixTree = t n.svalues = make([]*cf.Zp, t.NumSamples()) for i := 0; i < len(n.svalues); i++ { n.svalues[i] = cf.Zi(cf.P_SKS, 1) } }
// Test sync with polynomial interpolation. func (s *ReconSuite) TestPolySyncMBar(c *gc.C) { ptree1, cleanup, err := s.Factory() c.Assert(err, gc.IsNil) defer cleanup() ptree2, cleanup, err := s.Factory() c.Assert(err, gc.IsNil) defer cleanup() onlyInPeer1 := cf.NewZSet() // Load up peer 1 with items for i := 1; i < 100; i++ { ptree1.Insert(cf.Zi(cf.P_SKS, 65537*i)) } // Four extra samples for i := 1; i < 5; i++ { z := cf.Zi(cf.P_SKS, 68111*i) ptree1.Insert(z) onlyInPeer1.Add(z) } root, _ := ptree1.Root() c.Log("peer1:", recon.MustElements(root)) onlyInPeer2 := cf.NewZSet() // Load up peer 2 with items for i := 1; i < 100; i++ { ptree2.Insert(cf.Zi(cf.P_SKS, 65537*i)) } // One extra sample for i := 1; i < 2; i++ { z := cf.Zi(cf.P_SKS, 70001*i) ptree2.Insert(z) onlyInPeer2.Add(z) } root, _ = ptree2.Root() c.Log("peer2:", recon.MustElements(root)) port1, port2 := portPair(c) peer1 := s.newPeer(port1, port2, recon.PeerModeGossipOnly, ptree1) peer2 := s.newPeer(port2, port1, recon.PeerModeServeOnly, ptree2) err = s.pollConvergence(c, peer1, peer2, onlyInPeer2, onlyInPeer1, LongTimeout) c.Assert(err, gc.IsNil) }
func (s *PtreeSuite) TestInsertNodeSplit(c *gc.C) { tree := new(MemPrefixTree) tree.Init() // Add a bunch of nodes, enough to cause splits for i := 0; i < tree.SplitThreshold()*4; i++ { tree.Insert(cf.Zi(cf.P_SKS, i+65536)) } // Remove a bunch of nodes, enough to cause joins for i := 0; i < tree.SplitThreshold()*4; i++ { tree.Remove(cf.Zi(cf.P_SKS, i+65536)) } root, err := tree.Root() c.Assert(err, gc.IsNil) // Insert/Remove reversible after splitting & joining? for _, sv := range root.SValues() { c.Assert(sv.Cmp(cf.Zi(cf.P_SKS, 1)), gc.Equals, 0) } c.Assert(tree.root.children, gc.HasLen, 0) c.Assert(tree.root.elements, gc.HasLen, 0) }
func (s *PtreeSuite) TestInsertNodesNoSplit(c *gc.C) { s.ptree.Insert(cf.Zi(cf.P_SKS, 100)) s.ptree.Insert(cf.Zi(cf.P_SKS, 300)) s.ptree.Insert(cf.Zi(cf.P_SKS, 500)) root, err := s.ptree.Root() c.Assert(err, gc.IsNil) c.Assert(recon.MustElements(root), gc.HasLen, 3) c.Assert(root.IsLeaf(), gc.Equals, true) s.ptree.Remove(cf.Zi(cf.P_SKS, 100)) s.ptree.Remove(cf.Zi(cf.P_SKS, 300)) s.ptree.Remove(cf.Zi(cf.P_SKS, 500)) root, err = s.ptree.Root() c.Assert(recon.MustElements(root), gc.HasLen, 0) for _, sv := range root.SValues() { c.Assert(sv.Cmp(cf.Zi(cf.P_SKS, 1)), gc.Equals, 0) } }