@@ -35,9 +35,7 @@ func TestManager(t *testing.T) {
3535
3636 network := "udp4"
3737 turnSocket , err := net .ListenPacket (network , "0.0.0.0:0" )
38- if err != nil {
39- panic (err )
40- }
38+ assert .NoError (t , err )
4139
4240 for _ , tc := range tt {
4341 f := tc .f
@@ -54,15 +52,17 @@ func subTestCreateInvalidAllocation(t *testing.T, turnSocket net.PacketConn) {
5452 m , err := newTestManager ()
5553 assert .NoError (t , err )
5654
57- if a , err := m .CreateAllocation (nil , turnSocket , 0 , proto .DefaultLifetime ); a != nil || err == nil {
58- t .Errorf ("Illegally created allocation with nil FiveTuple" )
59- }
60- if a , err := m .CreateAllocation (randomFiveTuple (), nil , 0 , proto .DefaultLifetime ); a != nil || err == nil {
61- t .Errorf ("Illegally created allocation with nil turnSocket" )
62- }
63- if a , err := m .CreateAllocation (randomFiveTuple (), turnSocket , 0 , 0 ); a != nil || err == nil {
64- t .Errorf ("Illegally created allocation with 0 lifetime" )
65- }
55+ a , err := m .CreateAllocation (nil , turnSocket , 0 , proto .DefaultLifetime )
56+ assert .Nil (t , a , "Illegally created allocation with nil FiveTuple" )
57+ assert .Error (t , err , "Illegally created allocation with nil FiveTuple" )
58+
59+ a , err = m .CreateAllocation (randomFiveTuple (), nil , 0 , proto .DefaultLifetime )
60+ assert .Nil (t , a , "Illegally created allocation with nil turnSocket" )
61+ assert .Error (t , err , "Illegally created allocation with nil turnSocket" )
62+
63+ a , err = m .CreateAllocation (randomFiveTuple (), turnSocket , 0 , 0 )
64+ assert .Nil (t , a , "Illegally created allocation with 0 lifetime" )
65+ assert .Error (t , err , "Illegally created allocation with 0 lifetime" )
6666}
6767
6868// Test valid Allocation creations.
@@ -73,13 +73,12 @@ func subTestCreateAllocation(t *testing.T, turnSocket net.PacketConn) {
7373 assert .NoError (t , err )
7474
7575 fiveTuple := randomFiveTuple ()
76- if a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime ); a == nil || err != nil {
77- t . Errorf ( "Failed to create allocation %v %v" , a , err )
78- }
76+ a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime )
77+ assert . NotNil ( t , a , "Failed to create allocation" )
78+ assert . NoError ( t , err , "Failed to create allocation" )
7979
80- if a := m .GetAllocation (fiveTuple ); a == nil {
81- t .Errorf ("Failed to get allocation right after creation" )
82- }
80+ a = m .GetAllocation (fiveTuple )
81+ assert .NotNil (t , a , "Failed to get allocation right after creation" )
8382}
8483
8584// Test that two allocations can't be created with the same FiveTuple.
@@ -90,13 +89,13 @@ func subTestCreateAllocationDuplicateFiveTuple(t *testing.T, turnSocket net.Pack
9089 assert .NoError (t , err )
9190
9291 fiveTuple := randomFiveTuple ()
93- if a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime ); a == nil || err != nil {
94- t . Errorf ( "Failed to create allocation %v %v" , a , err )
95- }
92+ a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime )
93+ assert . NotNil ( t , a , "Failed to create allocation" )
94+ assert . NoError ( t , err , "Failed to create allocation" )
9695
97- if a , err : = m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime ); a != nil || err == nil {
98- t . Errorf ( "Was able to create allocation with same FiveTuple twice" )
99- }
96+ a , err = m .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime )
97+ assert . Nil ( t , a , "Was able to create allocation with same FiveTuple twice" )
98+ assert . Error ( t , err , "Was able to create allocation with same FiveTuple twice" )
10099}
101100
102101func subTestDeleteAllocation (t * testing.T , turnSocket net.PacketConn ) {
@@ -106,18 +105,16 @@ func subTestDeleteAllocation(t *testing.T, turnSocket net.PacketConn) {
106105 assert .NoError (t , err )
107106
108107 fiveTuple := randomFiveTuple ()
109- if a , err := manager .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime ); a == nil || err != nil {
110- t . Errorf ( "Failed to create allocation %v %v" , a , err )
111- }
108+ a , err := manager .CreateAllocation (fiveTuple , turnSocket , 0 , proto .DefaultLifetime )
109+ assert . NotNil ( t , a , "Failed to create allocation" )
110+ assert . NoError ( t , err , "Failed to create allocation" )
112111
113- if a := manager .GetAllocation (fiveTuple ); a == nil {
114- t .Errorf ("Failed to get allocation right after creation" )
115- }
112+ a = manager .GetAllocation (fiveTuple )
113+ assert .NotNil (t , a , "Failed to get allocation right after creation" )
116114
117115 manager .DeleteAllocation (fiveTuple )
118- if a := manager .GetAllocation (fiveTuple ); a != nil {
119- t .Errorf ("Get allocation with %v should be nil after delete" , fiveTuple )
120- }
116+ a = manager .GetAllocation (fiveTuple )
117+ assert .Nilf (t , a , "Failed to delete allocation %v" , fiveTuple )
121118}
122119
123120// Test that allocation should be closed if timeout.
@@ -134,19 +131,15 @@ func subTestAllocationTimeout(t *testing.T, turnSocket net.PacketConn) {
134131 fiveTuple := randomFiveTuple ()
135132
136133 a , err := m .CreateAllocation (fiveTuple , turnSocket , 0 , lifetime )
137- if err != nil {
138- t .Errorf ("Failed to create allocation with %v" , fiveTuple )
139- }
134+ assert .NoErrorf (t , err , "Failed to create allocation with %v" , fiveTuple )
140135
141136 allocations [index ] = a
142137 }
143138
144139 // Make sure all allocations timeout
145140 time .Sleep (lifetime + time .Second )
146141 for _ , alloc := range allocations {
147- if ! isClose (alloc .RelaySocket ) {
148- t .Error ("Allocation relay socket should be closed if lifetime timeout" )
149- }
142+ assert .True (t , isClose (alloc .RelaySocket ), "Allocation relay socket should be closed if lifetime timeout" )
150143 }
151144}
152145
@@ -166,15 +159,10 @@ func subTestManagerClose(t *testing.T, turnSocket net.PacketConn) {
166159
167160 // Make a1 timeout
168161 time .Sleep (2 * time .Second )
169-
170- if err := manager .Close (); err != nil {
171- t .Errorf ("Manager close with error: %v" , err )
172- }
162+ assert .NoError (t , manager .Close ())
173163
174164 for _ , alloc := range allocations {
175- if ! isClose (alloc .RelaySocket ) {
176- t .Error ("Manager's allocations should be closed" )
177- }
165+ assert .True (t , isClose (alloc .RelaySocket ), "Manager's allocations should be closed" )
178166 }
179167}
180168
0 commit comments