You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.1 KiB
62 lines
1.1 KiB
package model |
|
|
|
import ( |
|
"fmt" |
|
"testing" |
|
"time" |
|
) |
|
|
|
func TestQueue(t *testing.T) { |
|
queue := newMessageSet(20) |
|
finish := make(chan bool, 2) |
|
|
|
fnPush := func() { |
|
for k := 0; k < 30; k++ { |
|
msg := &BroadcastMessage{ |
|
Timestamp: ISOTime{time.Now()}, |
|
Message: fmt.Sprintf("Message %02d", k), |
|
} |
|
ok := queue.put(msg) |
|
if !ok { |
|
t.Logf("Push error (#n = %d)\n", queue.count()) |
|
time.Sleep(20) |
|
} |
|
} |
|
finish <- true |
|
} |
|
|
|
fnPop := func() { |
|
for k := 0; k < 50; k++ { |
|
msg := queue.take() |
|
if msg != nil { |
|
t.Logf("#Items %d, msg: %v -> %v", queue.count(), msg.Timestamp, msg.Message) |
|
} else { |
|
t.Logf("#num items %d", queue.count()) |
|
time.Sleep(1) |
|
} |
|
} |
|
finish <- true |
|
close(finish) |
|
} |
|
|
|
go fnPush() |
|
go fnPop() |
|
|
|
for ok := range finish { |
|
t.Logf("Finish %v", ok) |
|
} |
|
|
|
//pointer test |
|
msg1 := &BroadcastMessage{ |
|
Timestamp: ISOTime{time.Now()}, |
|
} |
|
msg2 := msg1 |
|
msg3 := msg1 |
|
|
|
if msg2 == msg3 { |
|
t.Logf("Message is equal") |
|
} else { |
|
t.Logf("Message is not equal") |
|
} |
|
|
|
}
|
|
|