本文整理汇总了Golang中github.com/stretchr/testify/assert.NotPanics函数的典型用法代码示例。如果您正苦于以下问题:Golang NotPanics函数的具体用法?Golang NotPanics怎么用?Golang NotPanics使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NotPanics函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestSendPings
func TestSendPings(t *testing.T) {
d := newDefaultDaemon()
defer shutdown(d)
c := gnetConnection(addr)
go d.Pool.Pool.ConnectionWriteLoop(c)
d.Pool.Pool.Pool[1] = c
assert.NotPanics(t, d.Pool.sendPings)
wait()
assert.Equal(t, len(d.Pool.Pool.SendResults), 1)
if len(d.Pool.Pool.SendResults) == 0 {
t.Fatal("SendResults empty, would block")
}
sr := <-d.Pool.Pool.SendResults
assert.Equal(t, sr.Connection, c)
assert.Nil(t, sr.Error)
_, ok := sr.Message.(*PingMessage)
assert.True(t, ok)
assert.False(t, c.LastSent.IsZero())
// No pings should be sent, since we just pinged
lastSent := c.LastSent
assert.NotPanics(t, d.Pool.sendPings)
wait()
assert.Equal(t, len(d.Pool.Pool.SendResults), 0)
assert.Equal(t, c.LastSent, lastSent)
}
示例2: TestCacheExtractorRemoteServer
func TestCacheExtractorRemoteServer(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(testServeCache))
defer ts.Close()
defer os.Remove(cacheExtractorArchive)
defer os.Remove(cacheExtractorTestArchivedFile)
os.Remove(cacheExtractorArchive)
os.Remove(cacheExtractorTestArchivedFile)
helpers.MakeFatalToPanic()
cmd := CacheExtractorCommand{
File: cacheExtractorArchive,
URL: ts.URL + "/cache.zip",
}
assert.NotPanics(t, func() {
cmd.Execute(nil)
})
_, err := os.Stat(cacheExtractorTestArchivedFile)
assert.NoError(t, err)
os.Chtimes(cacheExtractorArchive, time.Now().Add(time.Hour), time.Now().Add(time.Hour))
assert.NotPanics(t, func() {
cmd.Execute(nil)
}, "archive is up to date")
}
示例3: TestMiddleware
// TestMiddleware runs
func TestMiddleware(t *testing.T) {
assert := assert.New(t)
mw := Middleware{}
mw.Func = func() {
}
assert.Panics(func() {
mw.HandlerFunc()
})
ProdModeClosure(func() {
assert.NotPanics(func() {
mw.HandlerFunc()
})
})
mw.Func = func(c *gin.Context) {
}
assert.IsType(*new(gin.HandlerFunc), mw.HandlerFunc())
mw.Func = func(c Context) {
}
assert.IsType(*new(gin.HandlerFunc), mw.HandlerFunc())
assert.NotPanics(func() {
mw.HandlerFunc()(nil)
})
mw = LoggerMiddleware()
assert.IsType(*new(gin.HandlerFunc), mw.HandlerFunc())
}
示例4: TestVisorShutdown
func TestVisorShutdown(t *testing.T) {
defer cleanupVisor()
c, _ := setupVisor()
c.Disabled = true
c.Config.BlockchainFile = testBlockchainFile
c.Config.BlockSigsFile = testBlocksigsFile
c.Config.WalletFile = testWalletFile
v := NewVisor(c)
assert.NotPanics(t, v.Shutdown)
// Should not save anything
assertFileNotExists(t, testBlockchainFile)
assertFileNotExists(t, testBlocksigsFile)
assertFileNotExists(t, testWalletFile)
cleanupVisor()
c.Disabled = false
v = NewVisor(c)
assert.NotPanics(t, v.Shutdown)
assertFileExists(t, testBlockchainFile)
assertFileExists(t, testBlocksigsFile)
assertFileExists(t, testWalletFile)
cleanupVisor()
// If master, no wallet should be saved
c = setupMasterVisor()
c.Config.BlockchainFile = testBlockchainFile
c.Config.BlockSigsFile = testBlocksigsFile
c.Config.WalletFile = testWalletFile
v = NewVisor(c)
assert.NotPanics(t, v.Shutdown)
assertFileExists(t, testBlockchainFile)
assertFileExists(t, testBlocksigsFile)
assertFileNotExists(t, testWalletFile)
cleanupVisor()
}
示例5: TestConnectToRandomPeer
func TestConnectToRandomPeer(t *testing.T) {
d := newDefaultDaemon()
defer shutdown(d)
defer cleanupPeers()
// Disabled
d.Config.DisableOutgoingConnections = true
assert.NotPanics(t, d.connectToRandomPeer)
assert.Equal(t, len(d.pendingConnections), 0)
// Enabled, but only private peers
addr := "192.168.1.196:30954"
p, err := d.Peers.Peers.AddPeer(addr)
assert.Nil(t, err)
p.Private = true
d.Config.DisableOutgoingConnections = false
assert.NotPanics(t, d.connectToRandomPeer)
assert.Equal(t, len(d.pendingConnections), 0)
// Enabled, and we have a public peer
p.Private = false
assert.NotPanics(t, d.connectToRandomPeer)
assert.Equal(t, len(d.pendingConnections), 1)
assert.NotNil(t, d.pendingConnections[addr])
}
示例6: TestAuthenticationSetUp
func TestAuthenticationSetUp(t *testing.T) {
assert.Panics(t, func() {
Authentication([]byte(""))
})
assert.NotPanics(t, func() {
Authentication(TEST_AUTH_KEY)
})
validTokenText, err := mockLoginToken("10", time.Now().Add(10*time.Second), TEST_AUTH_KEY)
assert.NoError(t, err)
handler := Authentication(TEST_AUTH_KEY)
// Run the handler with a valid token
c := createGinContext(validTokenText)
assert.NotPanics(t, func() {
handler(c)
})
assert.Empty(t, c.Errors)
assert.Equal(t, "10", c.MustGet(AuthUserIDKey))
assert.IsType(t, &jwt.Token{}, c.MustGet(AuthTokenKey))
// Run the handler with invalid token
// There is no easy way to create a gin writer to properly detect response
// Go will panic when Authentication tries to add error to an empty Writer object
// So we can assume that the validation failed and user is not allowed through :)
c = createGinContext("RANDOM TOKEN TEXT")
assert.Panics(t, func() {
handler(c)
})
}
示例7: 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())
}
示例8: 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())
}
示例9: TestInitPeers
func TestInitPeers(t *testing.T) {
defer cleanupPeers()
c := NewPeersConfig()
peers := NewPeers(c)
// Write dummy peer db
fn := "./" + pex.PeerDatabaseFilename
cleanupPeers()
f, err := os.Create(fn)
assert.Nil(t, err)
if err != nil {
t.Fatalf("Error creating %s", fn)
}
_, err = f.Write([]byte(addr + " 0 0\n"))
assert.Nil(t, err)
f.Close()
peers.Config.DataDirectory = "./"
assert.NotPanics(t, func() { peers.Init() })
assert.Equal(t, len(peers.Peers.Peerlist), 1)
assert.NotNil(t, peers.Peers.Peerlist[addr])
assert.False(t, peers.Peers.AllowLocalhost)
peers.Config.AllowLocalhost = true
assert.NotPanics(t, func() { peers.Init() })
assert.True(t, peers.Peers.AllowLocalhost)
}
示例10: 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())
}
示例11: 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)
}
示例12: 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()
}
示例13: TestMustVersionByName
func TestMustVersionByName(t *testing.T) {
assert.Panics(t, func() { SetAddressVersion("bad") })
for k, _ := range addressVersions {
assert.NotPanics(t, func() { SetAddressVersion(k) })
}
assert.NotPanics(t, func() { SetAddressVersion("main") })
assert.NotPanics(t, func() { SetAddressVersion("test") })
}
示例14: TestSingleUse
func TestSingleUse(t *testing.T) {
var s singleUse
assert.NotPanics(t, func() { s.ensureUnused("foo") })
assert.NotPanics(t, func() { s.use("foo") })
assert.Panics(t, func() { s.ensureUnused("foo") })
assert.Panics(t, func() { s.use("foo") })
}
示例15: 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())
})
}