summaryrefslogtreecommitdiff
path: root/plugins/jobs/oooold/broker/amqp/stat_test.go
blob: ef19746c114fea8a13c2c2f6cd6d1d2f69bebc19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package amqp

import (
	"github.com/spiral/jobs/v2"
	"github.com/stretchr/testify/assert"
	"sync"
	"testing"
)

func TestBroker_Stat(t *testing.T) {
	b := &Broker{}
		_, err := b.Init(cfg)
	if err != nil {
		t.Fatal(err)
	}
	b.Register(pipe)

	ready := make(chan interface{})
	b.Listen(func(event int, ctx interface{}) {
		if event == jobs.EventBrokerReady {
			close(ready)
		}
	})

	exec := make(chan jobs.Handler, 1)

	go func() { assert.NoError(t, b.Serve()) }()
	defer b.Stop()

	<-ready

	jid, perr := b.Push(pipe, &jobs.Job{Job: "test", Payload: "body", Options: &jobs.Options{}})

	assert.NotEqual(t, "", jid)
	assert.NoError(t, perr)

	stat, err := b.Stat(pipe)
	assert.NoError(t, err)
	assert.Equal(t, int64(1), stat.Queue)
	assert.Equal(t, int64(0), stat.Active)

	assert.NoError(t, b.Consume(pipe, exec, func(id string, j *jobs.Job, err error) {}))

	wg := &sync.WaitGroup{}
	wg.Add(1)
	exec <- func(id string, j *jobs.Job) error {
		defer wg.Done()
		assert.Equal(t, jid, id)
		assert.Equal(t, "body", j.Payload)

		stat, err := b.Stat(pipe)
		assert.NoError(t, err)
		assert.Equal(t, int64(1), stat.Active)

		return nil
	}

	wg.Wait()
	stat, err = b.Stat(pipe)
	assert.NoError(t, err)
	assert.Equal(t, int64(0), stat.Queue)
	assert.Equal(t, int64(0), stat.Active)
}