make Item an interface{}

This commit is contained in:
Gregory 2021-11-01 23:00:17 +02:00
parent 6be93258a4
commit a3398a8900
2 changed files with 13 additions and 15 deletions

View file

@ -4,9 +4,7 @@
// (https://www.youtube.com/watch?v=5zXAHh5tJqQ)
package queue
type Item struct {
Val interface{}
}
type Item interface{}
// This type represents request for content from queue.
// When n number of items become avaliable in a queue

View file

@ -9,17 +9,17 @@ import (
func TestPut(t *testing.T) {
q := NewQueue()
q.Put(Item{1})
if q.Get().Val.(int) != 1 {
q.Put(1)
if q.Get().(int) != 1 {
t.Fatal("wrong item in queue")
}
}
func TestPutMultiple(t *testing.T) {
q := NewQueue()
q.Put(Item{1})
q.Put(Item{2})
first, second := q.Get().Val.(int), q.Get().Val.(int)
q.Put(1)
q.Put(2)
first, second := q.Get().(int), q.Get().(int)
if first != 1 || second != 2 {
t.Fatal("wrong item in queue or wrong order")
}
@ -50,7 +50,7 @@ func TestGetMany(t *testing.T) {
return c
}
q.Put(Item{1})
q.Put(1)
select {
case <-result2():
@ -59,14 +59,14 @@ func TestGetMany(t *testing.T) {
}
// this call unblocks first GetMany call and empties queue
q.Put(Item{2})
q.Put(2)
// Put enough items in queue for result2 not to block
q.Put(Item{3})
q.Put(Item{4})
q.Put(3)
q.Put(4)
select {
case res := <-result2():
third, fourth := res[0].Val.(int), res[1].Val.(int)
third, fourth := res[0].(int), res[1].(int)
if third != 3 || fourth != 4 {
t.Fatal("wrong item in queue or wrong order")
}
@ -85,14 +85,14 @@ func TestConcurrent(t *testing.T) {
for i := 0; i < 1000; i++ {
go func(i int) {
defer wg.Done()
q.Put(Item{i})
q.Put(i)
}(i)
}
for i := 0; i < 1000; i++ {
go func() {
defer wg.Done()
sum += q.Get().Val.(int)
sum += q.Get().(int)
}()
}