本文整理汇总了Golang中github.com/skycoin/gnet.EraseMessages函数的典型用法代码示例。如果您正苦于以下问题:Golang EraseMessages函数的具体用法?Golang EraseMessages怎么用?Golang EraseMessages使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EraseMessages函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestVisorAnnounceBlocks
func TestVisorAnnounceBlocks(t *testing.T) {
defer cleanupVisor()
defer gnet.EraseMessages()
p, gc := setupPool()
vc, _ := setupVisor()
go p.Pool.ConnectionWriteLoop(gc)
// Disabled
vc.Disabled = true
v := NewVisor(vc)
assert.NotPanics(t, func() { v.AnnounceBlocks(p) })
wait()
assert.Equal(t, len(p.Pool.SendResults), 0)
assert.True(t, gc.LastSent.IsZero())
// Valid send
vc.Disabled = false
gc.Conn = NewDummyConn(addr)
v = NewVisor(vc)
assert.False(t, v.Config.Disabled)
assert.NotPanics(t, func() { v.AnnounceBlocks(p) })
wait()
assert.Equal(t, len(p.Pool.SendResults), 1)
if len(p.Pool.SendResults) == 0 {
t.Fatal("SendResults empty, would block")
}
sr := <-p.Pool.SendResults
assert.Nil(t, sr.Error)
assert.Equal(t, sr.Connection, gc)
_, ok := sr.Message.(*AnnounceBlocksMessage)
assert.True(t, ok)
assert.False(t, gc.LastSent.IsZero())
}
示例2: TestVisorBroadcastBlock
func TestVisorBroadcastBlock(t *testing.T) {
defer cleanupVisor()
defer gnet.EraseMessages()
p, gc := setupPool()
vc, _ := setupVisor()
vc.Disabled = true
v := NewVisor(vc)
// Should not send anything if disabled
assert.NotPanics(t, func() {
v.broadcastBlock(visor.SignedBlock{}, p)
})
assert.Equal(t, len(p.Pool.SendResults), 0)
assert.True(t, gc.LastSent.IsZero())
// Sending
gc.Conn = NewDummyConn(addr)
vc.Disabled = false
v = NewVisor(vc)
sb := v.Visor.GetGenesisBlock()
assert.NotPanics(t, func() {
v.broadcastBlock(sb, p)
})
go p.Pool.ConnectionWriteLoop(gc)
wait()
assert.Equal(t, len(p.Pool.SendResults), 1)
if len(p.Pool.SendResults) == 0 {
t.Fatal("SendResults empty, would block")
}
sr := <-p.Pool.SendResults
assert.Equal(t, sr.Connection, gc)
_, ok := sr.Message.(*GiveBlocksMessage)
assert.True(t, ok)
assert.Nil(t, sr.Error)
assert.False(t, gc.LastSent.IsZero())
}
示例3: TestOnDisconnect
func TestOnDisconnect(t *testing.T) {
gnet.EraseMessages()
d := newDefaultDaemon()
c := gnetConnection(addr)
var mirror uint32 = 100
// Not blacklistable
reason := gnet.DisconnectWriteFailed
setupTestOnDisconnect(d, c, mirror)
assert.NotPanics(t, func() { d.onGnetDisconnect(c, reason) })
// Should not be in blacklist
assert.Equal(t, len(d.Peers.Peers.Blacklist), 0)
// Should no longer be in OutgoingConnections
assert.Equal(t, len(d.OutgoingConnections), 0)
// Should no longer be in d.ExpectingIntroductions
assert.Equal(t, len(d.ExpectingIntroductions), 0)
// Should be removed from the mirror, and the mirror dict for this ip
// should be removed
assert.Equal(t, len(d.mirrorConnections), 0)
assert.Equal(t, len(d.ConnectionMirrors), 0)
// Blacklistable
reason = DisconnectIntroductionTimeout
setupTestOnDisconnect(d, c, mirror)
assert.NotPanics(t, func() { d.onGnetDisconnect(c, reason) })
assert.Equal(t, len(d.Peers.Peers.Blacklist), 1)
assert.NotNil(t, d.Peers.Peers.Blacklist[addr])
// Should be in blacklist
assert.Equal(t, len(d.Peers.Peers.Blacklist), 1)
assert.NotNil(t, d.Peers.Peers.Blacklist[addr])
// Should no longer be in OutgoingConnections
assert.Equal(t, len(d.OutgoingConnections), 0)
// Should no longer be in d.ExpectingIntroductions
assert.Equal(t, len(d.ExpectingIntroductions), 0)
// Should be removed from the mirror, and the mirror dict for this ip
// should be removed
assert.Equal(t, len(d.mirrorConnections), 0)
assert.Equal(t, len(d.ConnectionMirrors), 0)
// Cleanup
delete(d.Peers.Peers.Blacklist, addr)
// d.mirrorConnections should retain a submap if there are other ports
// inside
reason = gnet.DisconnectWriteFailed
setupTestOnDisconnect(d, c, mirror)
d.mirrorConnections[mirror][strings.Split(addrb, ":")[0]] = addrPort
assert.NotPanics(t, func() { d.onGnetDisconnect(c, reason) })
// Should not be in blacklist
assert.Equal(t, len(d.Peers.Peers.Blacklist), 0)
// Should no longer be in OutgoingConnections
assert.Equal(t, len(d.OutgoingConnections), 0)
// Should no longer be in d.ExpectingIntroductions
assert.Equal(t, len(d.ExpectingIntroductions), 0)
// Should be removed from the mirror, and the mirror dict for this ip
// should be removed
assert.Equal(t, len(d.mirrorConnections), 1)
assert.Equal(t, len(d.mirrorConnections[mirror]), 1)
assert.Equal(t, len(d.ConnectionMirrors), 0)
shutdown(d)
}
示例4: TestVisorBroadcastTransaction
func TestVisorBroadcastTransaction(t *testing.T) {
defer cleanupVisor()
defer gnet.EraseMessages()
p, gc := setupPool()
go p.Pool.ConnectionWriteLoop(gc)
vc, _ := setupVisor()
vc.Disabled = true
v := NewVisor(vc)
ut := createUnconfirmedTxn()
assert.NotPanics(t, func() {
v.broadcastTransaction(ut.Txn, p)
})
wait()
assert.Equal(t, len(p.Pool.SendResults), 0)
assert.True(t, gc.LastSent.IsZero())
// Sending
vc.Disabled = false
gc.Conn = NewDummyConn(addr)
v = NewVisor(vc)
assert.NotPanics(t, func() {
v.broadcastTransaction(ut.Txn, p)
})
wait()
assert.Equal(t, len(p.Pool.SendResults), 1)
if len(p.Pool.SendResults) == 0 {
t.Fatal("SendResults empty, would block")
}
sr := <-p.Pool.SendResults
assert.Nil(t, sr.Error)
assert.Equal(t, sr.Connection, gc)
_, ok := sr.Message.(*GiveTxnsMessage)
assert.True(t, ok)
assert.False(t, gc.LastSent.IsZero())
}
示例5: TestPingMessage
func TestPingMessage(t *testing.T) {
d := newDefaultDaemon()
defer shutdown(d)
p := d.Pool.Pool
m := &PingMessage{}
testSimpleMessageHandler(t, d, m)
m.c = messageContext(addr)
go p.ConnectionWriteLoop(m.c.Conn)
defer m.c.Conn.Close()
assert.NotPanics(t, func() { m.Process(d) })
// A pong message should have been sent
wait()
assert.Equal(t, len(p.SendResults), 1)
if len(p.SendResults) == 0 {
t.Fatalf("SendResults empty, would block")
}
sr := <-p.SendResults
assert.Equal(t, sr.Connection, m.c.Conn)
assert.Nil(t, sr.Error)
_, ok := sr.Message.(*PongMessage)
assert.True(t, ok)
assert.False(t, m.c.Conn.LastSent.IsZero())
// Test serialization
mm := PingMessage{}
b := encoder.Serialize(mm)
m2 := PingMessage{}
assert.Nil(t, encoder.DeserializeRaw(b, &m2))
assert.Equal(t, mm, m2)
gnet.EraseMessages()
}
示例6: TestGivePeersMessage
func TestGivePeersMessage(t *testing.T) {
d := newDefaultDaemon()
defer shutdown(d)
addrs := []string{addr, addrb, "7"}
peers := make([]*pex.Peer, 3)
for i, addr := range addrs {
peers[i] = &pex.Peer{Addr: addr}
}
m := NewGivePeersMessage(peers)
assert.Equal(t, len(m.GetPeers()), 2)
testSimpleMessageHandler(t, d, m)
assert.Equal(t, m.GetPeers()[0], addrs[0])
assert.Equal(t, m.GetPeers()[1], addrs[1])
// Test disabled
d.Peers.Config.Disabled = true
m.Process(d)
assert.Equal(t, len(d.Peers.Peers.Peerlist), 0)
// Test serialization
m = NewGivePeersMessage(peers)
b := encoder.Serialize(m)
m2 := GivePeersMessage{}
assert.Nil(t, encoder.DeserializeRaw(b, &m2))
assert.Equal(t, *m, m2)
// Peers should be added to the pex when processed
d.Peers.Config.Disabled = false
m.Process(d)
assert.Equal(t, len(d.Peers.Peers.Peerlist), 2)
gnet.EraseMessages()
}
示例7: Shutdown
// Terminates all subsystems safely. To stop the Daemon run loop, send a value
// over the quit channel provided to Init. The Daemon run lopp must be stopped
// before calling this function.
func (self *Daemon) Shutdown() {
self.DHT.Shutdown()
self.Pool.Shutdown()
self.Peers.Shutdown()
self.Visor.Shutdown()
gnet.EraseMessages()
}
示例8: TestGetPeersMessage
func TestGetPeersMessage(t *testing.T) {
d := newDefaultDaemon()
defer shutdown(d)
p := d.Pool
m := NewGetPeersMessage()
testSimpleMessageHandler(t, d, m)
d.Peers.Peers.AddPeer(addr)
q, err := d.Peers.Peers.AddPeer(addrb)
assert.Nil(t, err)
q.Private = true
d.Peers.Config.ReplyCount = 100
m.c = messageContext(addr)
// Peers disabled
d.Peers.Config.Disabled = true
assert.NotPanics(t, func() { m.Process(d) })
wait()
assert.Equal(t, len(p.Pool.SendResults), 0)
assert.True(t, m.c.Conn.LastSent.IsZero())
// Peers enabled
d.Peers.Config.Disabled = false
m.c = messageContext(addr)
defer m.c.Conn.Close()
go p.Pool.ConnectionWriteLoop(m.c.Conn)
assert.NotPanics(t, func() { m.Process(d) })
wait()
assert.Equal(t, len(p.Pool.SendResults), 1)
if len(p.Pool.SendResults) == 0 {
t.Fatal("SendResults empty, would block")
}
sr := <-p.Pool.SendResults
assert.Nil(t, sr.Error)
assert.Equal(t, sr.Connection, m.c.Conn)
msg, ok := sr.Message.(*GivePeersMessage)
assert.True(t, ok)
// Private peer should not be included
ipAddr, err := NewIPAddr(addr)
assert.Nil(t, err)
assert.Equal(t, msg.Peers, []IPAddr{ipAddr})
assert.False(t, m.c.Conn.LastSent.IsZero())
// If no peers, nothing should happen
m.c.Conn.LastSent = util.ZeroTime()
delete(d.Peers.Peers.Peerlist, addr)
assert.NotPanics(t, func() { m.Process(d) })
wait()
assert.Equal(t, len(p.Pool.SendResults), 0)
assert.True(t, m.c.Conn.LastSent.IsZero())
// Test serialization
m = NewGetPeersMessage()
b := encoder.Serialize(m)
m2 := GetPeersMessage{}
assert.Nil(t, encoder.DeserializeRaw(b, &m2))
assert.Equal(t, *m, m2)
gnet.EraseMessages()
}
示例9: TestVisorSpend
func TestVisorSpend(t *testing.T) {
defer cleanupVisor()
defer gnet.EraseMessages()
p, gc := setupPool()
go p.Pool.ConnectionWriteLoop(gc)
vc, mv := setupVisor()
vc.Disabled = true
v := NewVisor(vc)
// Spending while disabled
assert.NotPanics(t, func() {
_, err := v.Spend(visor.Balance{10e6, 0}, 0,
mv.Wallet.GetAddresses()[0], p)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Visor disabled")
wait()
assert.Equal(t, len(p.Pool.SendResults), 0)
assert.True(t, gc.LastSent.IsZero())
})
// Spending but spend fails (no money)
vc.Disabled = false
v = NewVisor(vc)
assert.NotPanics(t, func() {
_, err := v.Spend(visor.Balance{1000 * 10e6, 0}, 0,
mv.Wallet.GetAddresses()[0], p)
wait()
assert.NotNil(t, err)
assert.Equal(t, len(p.Pool.SendResults), 0)
assert.Equal(t, len(v.Visor.Unconfirmed.Txns), 0)
assert.True(t, gc.LastSent.IsZero())
})
// Spending succeeds, and announced
vc, mv = setupVisor()
vc.Disabled = false
gc.Conn = NewDummyConn(addr)
v = NewVisor(vc)
assert.Nil(t, transferCoins(mv, v.Visor))
assert.NotPanics(t, func() {
_, err := v.Spend(visor.Balance{10e6, 0}, 0,
mv.Wallet.GetAddresses()[0], p)
wait()
assert.Equal(t, len(p.Pool.SendResults), 1)
if len(p.Pool.SendResults) == 0 {
t.Fatal("SendResults empty, would block")
}
sr := <-p.Pool.SendResults
assert.Equal(t, sr.Connection, gc)
assert.Nil(t, sr.Error)
_, ok := sr.Message.(*GiveTxnsMessage)
assert.True(t, ok)
assert.Nil(t, err)
assert.Equal(t, len(v.Visor.Unconfirmed.Txns), 1)
assert.False(t, gc.LastSent.IsZero())
})
}
示例10: TestIntroductionMessageProcess
func TestIntroductionMessageProcess(t *testing.T) {
cleanupPeers()
d := newDefaultDaemon()
defer shutdown(d)
m := NewIntroductionMessage(d.Messages.Mirror, d.Config.Version,
uint16(poolPort))
m.c = messageContext(addr)
d.Pool.Pool.Pool[1] = m.c.Conn
// Test invalid
m.valid = false
d.ExpectingIntroductions[addr] = time.Now()
m.Process(d)
// d.ExpectingIntroductions should get updated
_, x := d.ExpectingIntroductions[addr]
assert.False(t, x)
// d.mirrorConnections should not have an entry
_, x = d.mirrorConnections[m.Mirror]
assert.False(t, x)
assert.Equal(t, len(d.Peers.Peers.Peerlist), 0)
// Test valid
m.valid = true
d.ExpectingIntroductions[addr] = time.Now()
m.Process(d)
// d.ExpectingIntroductions should get updated
_, x = d.ExpectingIntroductions[addr]
assert.False(t, x)
assert.Equal(t, len(d.Peers.Peers.Peerlist), 1)
assert.Equal(t, d.ConnectionMirrors[addr], m.Mirror)
assert.NotNil(t, d.mirrorConnections[m.Mirror])
assert.Equal(t, d.mirrorConnections[m.Mirror][addrIP], addrPort)
peerAddr := fmt.Sprintf("%s:%d", addrIP, poolPort)
assert.NotNil(t, d.Peers.Peers.Peerlist[peerAddr])
// Handle impossibly bad ip:port returned from conn.Addr()
// User should be disconnected
m.valid = true
m.c = messageContext(badAddrPort)
m.Process(d)
if len(d.Pool.Pool.DisconnectQueue) != 1 {
t.Fatalf("DisconnectQueue empty")
}
<-d.Pool.Pool.DisconnectQueue
m.valid = true
m.c = messageContext(badAddrNoPort)
m.Process(d)
if len(d.Pool.Pool.DisconnectQueue) != 1 {
t.Fatalf("DisconnectQueue empty")
}
<-d.Pool.Pool.DisconnectQueue
gnet.EraseMessages()
}
示例11: TestIntroductionMessageHandle
func TestIntroductionMessageHandle(t *testing.T) {
d := newDefaultDaemon()
defer shutdown(d)
mc := messageContext(addr)
m := NewIntroductionMessage(d.Messages.Mirror, d.Config.Version,
d.Pool.Pool.Config.Port)
// Test valid handling
m.Mirror = d.Messages.Mirror + 1
err := m.Handle(mc, d)
assert.Nil(t, err)
if len(d.messageEvents) == 0 {
t.Fatalf("messageEvent is empty")
}
<-d.messageEvents
assert.True(t, m.valid)
m.valid = false
// Test matching mirror
m.Mirror = d.Messages.Mirror
err = m.Handle(mc, d)
assert.Equal(t, err, DisconnectSelf)
m.Mirror = d.Messages.Mirror + 1
assert.False(t, m.valid)
// Test mismatched d.Config.Version
m.Version = d.Config.Version + 1
err = m.Handle(mc, d)
assert.Equal(t, err, DisconnectInvalidVersion)
assert.False(t, m.valid)
// Test serialization
m = NewIntroductionMessage(d.Messages.Mirror, d.Config.Version,
d.Pool.Pool.Config.Port)
b := encoder.Serialize(m)
m2 := IntroductionMessage{}
assert.Nil(t, encoder.DeserializeRaw(b, &m2))
assert.Equal(t, *m, m2)
// Test already connected
d.mirrorConnections[m.Mirror] = make(map[string]uint16)
d.mirrorConnections[m.Mirror][addrIP] = addrPort + 1
err = m.Handle(mc, d)
assert.Equal(t, err, DisconnectConnectedTwice)
delete(d.mirrorConnections, m.Mirror)
assert.False(t, m.valid)
for len(d.messageEvents) > 0 {
<-d.messageEvents
}
gnet.EraseMessages()
}
示例12: TestRequestPeers
func TestRequestPeers(t *testing.T) {
defer cleanupPeers()
gnet.EraseMessages()
defer gnet.EraseMessages()
// Disabled
c := NewPeersConfig()
c.Disabled = true
p := NewPeers(c)
assert.NotPanics(t, func() { p.requestPeers(nil) })
// Full
c.Disabled = false
c.Max = 1
p = NewPeers(c)
p.Init()
p.Peers.AddPeer(addr)
assert.NotPanics(t, func() { p.requestPeers(nil) })
// Not full, will send message
c.Max = 10
p = NewPeers(c)
p.Init()
p.Peers.AddPeer(addr)
pool, gc := setupPool()
go pool.Pool.ConnectionWriteLoop(gc)
defer gc.Close()
assert.NotPanics(t, func() { p.requestPeers(pool) })
wait()
assert.Equal(t, len(pool.Pool.SendResults), 1)
if len(pool.Pool.SendResults) == 0 {
t.Fatalf("SendResults empty, would block")
}
sr := <-pool.Pool.SendResults
assert.Equal(t, sr.Connection, gc)
assert.Nil(t, sr.Error)
_, ok := sr.Message.(*GetPeersMessage)
assert.True(t, ok)
}
示例13: TestVisorBroadcastOurTransactions
func TestVisorBroadcastOurTransactions(t *testing.T) {
defer cleanupVisor()
defer gnet.EraseMessages()
p, gc := setupPool()
vc, _ := setupVisor()
go p.Pool.ConnectionWriteLoop(gc)
// Disabled
vc.Disabled = true
v := NewVisor(vc)
assert.NotPanics(t, func() {
v.BroadcastOurTransactions(p)
})
wait()
assert.Equal(t, len(p.Pool.SendResults), 0)
assert.True(t, gc.LastSent.IsZero())
// With no transactions, nothing should be sent
vc.Disabled = false
vc.TransactionRebroadcastRate = time.Millisecond * 5
v = NewVisor(vc)
time.Sleep(time.Millisecond * 20)
assert.NotPanics(t, func() {
v.BroadcastOurTransactions(p)
})
wait()
assert.Equal(t, len(p.Pool.SendResults), 0)
assert.True(t, gc.LastSent.IsZero())
// We have a stale owned unconfirmed txn, it should be sent
gc.Conn = NewDummyConn(addr)
vc.Disabled = false
v = NewVisor(vc)
tx := addUnconfirmedTxn(v)
assert.Equal(t, len(v.Visor.Unconfirmed.Txns), 1)
assert.NotPanics(t, func() {
v.BroadcastOurTransactions(p)
})
wait()
assert.Equal(t, len(p.Pool.SendResults), 1)
if len(p.Pool.SendResults) == 0 {
t.Fatal("SendResults empty, would block")
}
sr := <-p.Pool.SendResults
assert.Nil(t, sr.Error)
assert.Equal(t, sr.Connection, gc)
msg, ok := sr.Message.(*AnnounceTxnsMessage)
assert.True(t, ok)
assert.Equal(t, msg.Txns, coin.Transactions{tx.Txn}.Hashes())
assert.False(t, gc.LastSent.IsZero())
}
示例14: TestVisorRequestBlocksFromAddr
func TestVisorRequestBlocksFromAddr(t *testing.T) {
defer cleanupVisor()
defer gnet.EraseMessages()
p, gc := setupPool()
vc, _ := setupVisor()
go p.Pool.ConnectionWriteLoop(gc)
// Disabled
vc.Disabled = true
v := NewVisor(vc)
assert.NotPanics(t, func() {
err := v.RequestBlocksFromAddr(p, addr)
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Visor disabled")
})
wait()
assert.Equal(t, len(p.Pool.SendResults), 0)
assert.True(t, gc.LastSent.IsZero())
vc.Disabled = false
v = NewVisor(vc)
assert.NotPanics(t, func() {
assert.Nil(t, v.RequestBlocksFromAddr(p, addr))
})
wait()
assert.Equal(t, len(p.Pool.SendResults), 1)
if len(p.Pool.SendResults) == 0 {
t.Fatal("SendResults empty, would block")
}
sr := <-p.Pool.SendResults
assert.Nil(t, sr.Error)
assert.Equal(t, sr.Connection, gc)
_, ok := sr.Message.(*GetBlocksMessage)
assert.True(t, ok)
assert.False(t, gc.LastSent.IsZero())
// No connection found for addr
gc.LastSent = util.ZeroTime()
gc.Conn = NewDummyConn(addr)
delete(p.Pool.Pool, gc.Id)
delete(p.Pool.Addresses, gc.Addr())
assert.NotPanics(t, func() {
assert.NotNil(t, v.RequestBlocksFromAddr(p, addr))
})
wait()
assert.Equal(t, len(p.Pool.SendResults), 0)
assert.True(t, gc.LastSent.IsZero())
}
示例15: TestPongMessage
func TestPongMessage(t *testing.T) {
cmsgs := NewMessagesConfig()
cmsgs.Register()
m := &PongMessage{}
// Pongs dont do anything
assert.Nil(t, m.Handle(messageContext(addr), nil))
gnet.EraseMessages()
// Test serialization
mm := PongMessage{}
b := encoder.Serialize(mm)
m2 := PongMessage{}
assert.Nil(t, encoder.DeserializeRaw(b, &m2))
assert.Equal(t, mm, m2)
}