summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/linux.yml1
-rwxr-xr-xMakefile7
-rw-r--r--plugins/jobs/brokers/amqp/consumer.go9
-rw-r--r--plugins/jobs/plugin.go28
-rw-r--r--tests/plugins/jobs/configs/.rr-jobs-init-no-amqp-global.yaml75
-rw-r--r--tests/plugins/jobs/configs/.rr-jobs-init.yaml7
-rw-r--r--tests/plugins/jobs/configs/.rr-jobs-list.yaml91
-rw-r--r--tests/plugins/jobs/configs/.rr-jobs-pause-resume-all.yaml78
-rw-r--r--tests/plugins/jobs/configs/.rr-jobs-pause-resume-amqp.yaml78
-rw-r--r--tests/plugins/jobs/configs/.rr-jobs-pause-resume-ephemeral.yaml44
-rw-r--r--tests/plugins/jobs/jobs_plugin_test.go242
11 files changed, 630 insertions, 30 deletions
diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml
index bb8191c1..583d97f3 100644
--- a/.github/workflows/linux.yml
+++ b/.github/workflows/linux.yml
@@ -97,6 +97,7 @@ jobs:
go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/broadcast_plugin.txt -covermode=atomic ./tests/plugins/broadcast
go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/websockets.txt -covermode=atomic ./tests/plugins/websockets
go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/ws_origin.txt -covermode=atomic ./plugins/websockets
+ go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage-ci/jobs_core.txt -covermode=atomic ./tests/plugins/jobs
docker-compose -f ./tests/env/docker-compose.yaml down
cat ./coverage-ci/*.txt > ./coverage-ci/summary.txt
diff --git a/Makefile b/Makefile
index 40a5aad2..2ce9bc8f 100755
--- a/Makefile
+++ b/Makefile
@@ -37,6 +37,7 @@ test_coverage:
go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage/broadcast_plugin.out -covermode=atomic ./tests/plugins/broadcast
go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage/ws_plugin.out -covermode=atomic ./tests/plugins/websockets
go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage/ws_origin.out -covermode=atomic ./plugins/websockets
+ go test -v -race -cover -tags=debug -coverpkg=./... -coverprofile=./coverage/jobs_core.out -covermode=atomic ./tests/plugins/jobs
cat ./coverage/*.out > ./coverage/summary.out
docker-compose -f tests/env/docker-compose.yaml down
@@ -71,6 +72,7 @@ test: ## Run application tests
go test -v -race -tags=debug ./tests/plugins/broadcast
go test -v -race -tags=debug ./tests/plugins/websockets
go test -v -race -tags=debug ./plugins/websockets
+ go test -v -race -tags=debug ./tests/plugins/jobs
docker-compose -f tests/env/docker-compose.yaml down
testGo1.17beta1: ## Run application tests
@@ -82,9 +84,10 @@ testGo1.17beta1: ## Run application tests
go1.17beta1 test -v -race -tags=debug ./pkg/worker_watcher
go1.17beta1 test -v -race -tags=debug ./pkg/bst
go1.17beta1 test -v -race -tags=debug ./pkg/priorityqueue
- go1.17beta1 test -v -race -tags=debug ./tests/plugins/http
go1.17beta1 test -v -race -tags=debug ./plugins/http/config
go1.17beta1 test -v -race -tags=debug ./plugins/server
+ go1.17beta1 test -v -race -tags=debug ./plugins/websockets
+ go1.17beta1 test -v -race -tags=debug ./tests/plugins/http
go1.17beta1 test -v -race -tags=debug ./tests/plugins/informer
go1.17beta1 test -v -race -tags=debug ./tests/plugins/reload
go1.17beta1 test -v -race -tags=debug ./tests/plugins/server
@@ -101,5 +104,5 @@ testGo1.17beta1: ## Run application tests
go1.17beta1 test -v -race -tags=debug ./tests/plugins/kv
go1.17beta1 test -v -race -tags=debug ./tests/plugins/websockets
go1.17beta1 test -v -race -tags=debug ./tests/plugins/broadcast
- go1.17beta1 test -v -race -tags=debug ./plugins/websockets
+ go1.17beta1 test -v -race -tags=debug ./tests/plugins/jobs
docker-compose -f tests/env/docker-compose.yaml down
diff --git a/plugins/jobs/brokers/amqp/consumer.go b/plugins/jobs/brokers/amqp/consumer.go
index 481e102a..c2807b54 100644
--- a/plugins/jobs/brokers/amqp/consumer.go
+++ b/plugins/jobs/brokers/amqp/consumer.go
@@ -422,6 +422,15 @@ func (j *JobsConsumer) Resume(p string) {
func (j *JobsConsumer) Stop() error {
j.stopCh <- struct{}{}
+
+ pipe := j.pipeline.Load().(*pipeline.Pipeline)
+ j.eh.Push(events.JobEvent{
+ Event: events.EventPipeStopped,
+ Driver: pipe.Driver(),
+ Pipeline: pipe.Name(),
+ Start: time.Now(),
+ Elapsed: 0,
+ })
return nil
}
diff --git a/plugins/jobs/plugin.go b/plugins/jobs/plugin.go
index 86289aba..c83078c3 100644
--- a/plugins/jobs/plugin.go
+++ b/plugins/jobs/plugin.go
@@ -3,9 +3,7 @@ package jobs
import (
"context"
"fmt"
- "runtime"
"sync"
- "sync/atomic"
"time"
endure "github.com/spiral/endure/pkg/container"
@@ -101,23 +99,6 @@ func (p *Plugin) Serve() chan error { //nolint:gocognit
errCh := make(chan error, 1)
const op = errors.Op("jobs_plugin_serve")
- // THIS IS TEST HELPERS, SHOULD BE DELETED IN THE RELEASES !!!!!!!!!!!!!!!!!!!!!!!! <-----------------------------------------------------
- var rate uint64
- go func() {
- tt := time.NewTicker(time.Second * 1)
- for { //nolint:gosimple
- select {
- case <-tt.C:
- fmt.Printf("---> rate is: %d\n", atomic.LoadUint64(&rate))
- fmt.Printf("---> goroutines: %d\n", runtime.NumGoroutine())
- fmt.Printf("---> curr len: %d\n", p.queue.Len())
- atomic.StoreUint64(&rate, 0)
- }
- }
- }()
-
- // THIS IS TEST HELPERS, SHOULD BE DELETED IN THE RELEASES !!!!!!!!!!!!!!!!!!!!!!!! <-----------------------------------------------------
-
// register initial pipelines
p.pipelines.Range(func(key, value interface{}) bool {
t := time.Now()
@@ -234,9 +215,6 @@ func (p *Plugin) Serve() chan error { //nolint:gocognit
}
p.RUnlock()
- // TEST HELPER, SHOULD BE DELETED IN THE RELEASE <-----------------------------------------------------
- atomic.AddUint64(&rate, 1)
-
errAck := job.Ack()
if errAck != nil {
p.log.Error("acknowledge failed", "error", errAck)
@@ -269,7 +247,7 @@ func (p *Plugin) Stop() error {
}
}()
- // just wait pollers for 2 seconds before exit
+ // just wait pollers for 5 seconds before exit
time.Sleep(time.Second * 5)
return nil
@@ -470,6 +448,10 @@ func (p *Plugin) Destroy(pp string) error {
return errors.E(op, errors.Errorf("consumer not registered for the requested driver: %s", ppl.Driver()))
}
+ // delete consumer
+ delete(p.consumers, ppl.Name())
+ p.pipelines.Delete(pp)
+
return d.Stop()
}
diff --git a/tests/plugins/jobs/configs/.rr-jobs-init-no-amqp-global.yaml b/tests/plugins/jobs/configs/.rr-jobs-init-no-amqp-global.yaml
new file mode 100644
index 00000000..0d141b2b
--- /dev/null
+++ b/tests/plugins/jobs/configs/.rr-jobs-init-no-amqp-global.yaml
@@ -0,0 +1,75 @@
+rpc:
+ listen: tcp://127.0.0.1:6001
+
+server:
+ command: "php ../../client.php echo pipes"
+ relay: "pipes"
+ relay_timeout: "20s"
+
+logs:
+ level: error
+ mode: development
+
+jobs:
+ # num logical cores by default
+ num_pollers: 10
+ # 1mi by default
+ pipeline_size: 100000
+ # worker pool configuration
+ pool:
+ num_workers: 10
+ max_jobs: 0
+ allocate_timeout: 60s
+ destroy_timeout: 60s
+
+ # list of broker pipelines associated with endpoints
+ pipelines:
+ test-local:
+ driver: ephemeral
+ priority: 10
+ pipeline_size: 10000
+
+ test-local-2:
+ driver: ephemeral
+ priority: 1
+ pipeline_size: 10000
+
+ test-local-3:
+ driver: ephemeral
+ priority: 2
+ pipeline_size: 10000
+
+ test-1:
+ driver: amqp
+ priority: 1
+ pipeline_size: 1000000
+ queue: test-1-queue
+ exchange: default
+ exchange_type: direct
+ routing_key: test
+
+ test-2-amqp:
+ driver: amqp
+ priority: 2
+ pipeline_size: 100000
+ queue: test-2-queue
+ exchange: default
+ exchange_type: direct
+ routing_key: test-2
+
+ test-2:
+ driver: beanstalk
+ priority: 11
+ tube: default
+ pipeline_size: 1000000
+
+ test-3:
+ # priority: 11 - not defined, 10 by default
+ # driver locality not specified, local by default
+ driver: sqs
+ pipeline_size: 1000000
+ queue: default
+
+ # list of pipelines to be consumed by the server, keep empty if you want to start consuming manually
+ consume: [ "test-local", "test-local-2", "test-local-3", "test-1", "test-2-amqp" ]
+
diff --git a/tests/plugins/jobs/configs/.rr-jobs-init.yaml b/tests/plugins/jobs/configs/.rr-jobs-init.yaml
index 6efc760b..80826acc 100644
--- a/tests/plugins/jobs/configs/.rr-jobs-init.yaml
+++ b/tests/plugins/jobs/configs/.rr-jobs-init.yaml
@@ -1,5 +1,5 @@
rpc:
- listen: unix:///tmp/rr.sock
+ listen: tcp://127.0.0.1:6001
server:
command: "php ../../client.php echo pipes"
@@ -23,12 +23,13 @@ sqs:
MessageRetentionPeriod: 86400
logs:
- level: debug
+ level: info
+ encoding: console
mode: development
jobs:
# num logical cores by default
- num_pollers: 64
+ num_pollers: 10
# 1mi by default
pipeline_size: 100000
# worker pool configuration
diff --git a/tests/plugins/jobs/configs/.rr-jobs-list.yaml b/tests/plugins/jobs/configs/.rr-jobs-list.yaml
new file mode 100644
index 00000000..3d22a098
--- /dev/null
+++ b/tests/plugins/jobs/configs/.rr-jobs-list.yaml
@@ -0,0 +1,91 @@
+rpc:
+ listen: tcp://127.0.0.1:6001
+
+server:
+ command: "php ../../client.php echo pipes"
+ relay: "pipes"
+ relay_timeout: "20s"
+
+amqp:
+ addr: amqp://guest:guest@localhost:5672/
+
+ # beanstalk configuration
+beanstalk:
+ addr: tcp://localhost:11300
+
+ # amazon sqs configuration
+sqs:
+ key: api-key
+ secret: api-secret
+ region: us-west-1
+ endpoint: http://localhost:9324
+ declare:
+ MessageRetentionPeriod: 86400
+
+logs:
+ level: debug
+ mode: development
+
+jobs:
+ # num logical cores by default
+ num_pollers: 10
+ # 1mi by default
+ pipeline_size: 100000
+ # worker pool configuration
+ pool:
+ num_workers: 10
+ max_jobs: 0
+ allocate_timeout: 60s
+ destroy_timeout: 60s
+
+ # list of broker pipelines associated with endpoints
+ pipelines:
+ test-local:
+ driver: ephemeral
+ priority: 10
+ pipeline_size: 10000
+
+ test-local-2:
+ driver: ephemeral
+ priority: 1
+ pipeline_size: 10000
+
+ test-local-3:
+ driver: ephemeral
+ priority: 2
+ pipeline_size: 10000
+
+ test-1:
+ driver: amqp
+ priority: 1
+ pipeline_size: 1000000
+ queue: test-1-queue
+ exchange: default
+ exchange_type: direct
+ routing_key: test
+
+ test-2-amqp:
+ driver: amqp
+ priority: 2
+ pipeline_size: 100000
+ queue: test-2-queue
+ exchange: default
+ exchange_type: direct
+ routing_key: test-2
+
+ test-2:
+ driver: beanstalk
+ priority: 11
+ tube: default
+ pipeline_size: 1000000
+
+ test-3:
+ # priority: 11 - not defined, 10 by default
+ # driver locality not specified, local by default
+ driver: sqs
+ pipeline_size: 1000000
+ queue: default
+
+ # list of pipelines to be consumed by the server, keep empty if you want to start consuming manually
+ consume: [ "test-local", "test-local-2", "test-local-3", "test-1", "test-2-amqp" ]
+
diff --git a/tests/plugins/jobs/configs/.rr-jobs-pause-resume-all.yaml b/tests/plugins/jobs/configs/.rr-jobs-pause-resume-all.yaml
new file mode 100644
index 00000000..8789e872
--- /dev/null
+++ b/tests/plugins/jobs/configs/.rr-jobs-pause-resume-all.yaml
@@ -0,0 +1,78 @@
+rpc:
+ listen: tcp://127.0.0.1:6001
+
+server:
+ command: "php ../../client.php echo pipes"
+ relay: "pipes"
+ relay_timeout: "20s"
+
+amqp:
+ addr: amqp://guest:guest@localhost:5672/
+
+logs:
+ level: debug
+ mode: development
+
+jobs:
+ # num logical cores by default
+ num_pollers: 10
+ # 1mi by default
+ pipeline_size: 100000
+ # worker pool configuration
+ pool:
+ num_workers: 10
+ max_jobs: 0
+ allocate_timeout: 60s
+ destroy_timeout: 60s
+
+ # list of broker pipelines associated with endpoints
+ pipelines:
+ test-local:
+ driver: ephemeral
+ priority: 10
+ pipeline_size: 10000
+
+ test-local-2:
+ driver: ephemeral
+ priority: 1
+ pipeline_size: 10000
+
+ test-local-3:
+ driver: ephemeral
+ priority: 2
+ pipeline_size: 10000
+
+ test-1:
+ driver: amqp
+ priority: 1
+ pipeline_size: 1000000
+ queue: test-1-queue
+ exchange: default
+ exchange_type: direct
+ routing_key: test
+
+ test-2-amqp:
+ driver: amqp
+ priority: 2
+ pipeline_size: 100000
+ queue: test-2-queue
+ exchange: default
+ exchange_type: direct
+ routing_key: test-2
+
+ test-2:
+ driver: beanstalk
+ priority: 11
+ tube: default
+ pipeline_size: 1000000
+
+ test-3:
+ # priority: 11 - not defined, 10 by default
+ # driver locality not specified, local by default
+ driver: sqs
+ pipeline_size: 1000000
+ queue: default
+
+ # list of pipelines to be consumed by the server, keep empty if you want to start consuming manually
+ consume: [ "test-local", "test-local-2", "test-local-3", "test-1", "test-2-amqp" ]
+
diff --git a/tests/plugins/jobs/configs/.rr-jobs-pause-resume-amqp.yaml b/tests/plugins/jobs/configs/.rr-jobs-pause-resume-amqp.yaml
new file mode 100644
index 00000000..8789e872
--- /dev/null
+++ b/tests/plugins/jobs/configs/.rr-jobs-pause-resume-amqp.yaml
@@ -0,0 +1,78 @@
+rpc:
+ listen: tcp://127.0.0.1:6001
+
+server:
+ command: "php ../../client.php echo pipes"
+ relay: "pipes"
+ relay_timeout: "20s"
+
+amqp:
+ addr: amqp://guest:guest@localhost:5672/
+
+logs:
+ level: debug
+ mode: development
+
+jobs:
+ # num logical cores by default
+ num_pollers: 10
+ # 1mi by default
+ pipeline_size: 100000
+ # worker pool configuration
+ pool:
+ num_workers: 10
+ max_jobs: 0
+ allocate_timeout: 60s
+ destroy_timeout: 60s
+
+ # list of broker pipelines associated with endpoints
+ pipelines:
+ test-local:
+ driver: ephemeral
+ priority: 10
+ pipeline_size: 10000
+
+ test-local-2:
+ driver: ephemeral
+ priority: 1
+ pipeline_size: 10000
+
+ test-local-3:
+ driver: ephemeral
+ priority: 2
+ pipeline_size: 10000
+
+ test-1:
+ driver: amqp
+ priority: 1
+ pipeline_size: 1000000
+ queue: test-1-queue
+ exchange: default
+ exchange_type: direct
+ routing_key: test
+
+ test-2-amqp:
+ driver: amqp
+ priority: 2
+ pipeline_size: 100000
+ queue: test-2-queue
+ exchange: default
+ exchange_type: direct
+ routing_key: test-2
+
+ test-2:
+ driver: beanstalk
+ priority: 11
+ tube: default
+ pipeline_size: 1000000
+
+ test-3:
+ # priority: 11 - not defined, 10 by default
+ # driver locality not specified, local by default
+ driver: sqs
+ pipeline_size: 1000000
+ queue: default
+
+ # list of pipelines to be consumed by the server, keep empty if you want to start consuming manually
+ consume: [ "test-local", "test-local-2", "test-local-3", "test-1", "test-2-amqp" ]
+
diff --git a/tests/plugins/jobs/configs/.rr-jobs-pause-resume-ephemeral.yaml b/tests/plugins/jobs/configs/.rr-jobs-pause-resume-ephemeral.yaml
new file mode 100644
index 00000000..dc5bc3a1
--- /dev/null
+++ b/tests/plugins/jobs/configs/.rr-jobs-pause-resume-ephemeral.yaml
@@ -0,0 +1,44 @@
+rpc:
+ listen: tcp://127.0.0.1:6001
+
+server:
+ command: "php ../../client.php echo pipes"
+ relay: "pipes"
+ relay_timeout: "20s"
+
+logs:
+ level: info
+ mode: development
+
+jobs:
+ # num logical cores by default
+ num_pollers: 10
+ # 1mi by default
+ pipeline_size: 100000
+ # worker pool configuration
+ pool:
+ num_workers: 10
+ max_jobs: 0
+ allocate_timeout: 60s
+ destroy_timeout: 60s
+
+ # list of broker pipelines associated with endpoints
+ pipelines:
+ test-local:
+ driver: ephemeral
+ priority: 10
+ pipeline_size: 10000
+
+ test-local-2:
+ driver: ephemeral
+ priority: 1
+ pipeline_size: 10000
+
+ test-local-3:
+ driver: ephemeral
+ priority: 2
+ pipeline_size: 10000
+
+ # list of pipelines to be consumed by the server, keep empty if you want to start consuming manually
+ consume: [ "test-local", "test-local-2" ]
+
diff --git a/tests/plugins/jobs/jobs_plugin_test.go b/tests/plugins/jobs/jobs_plugin_test.go
index 76b7b879..034ffc45 100644
--- a/tests/plugins/jobs/jobs_plugin_test.go
+++ b/tests/plugins/jobs/jobs_plugin_test.go
@@ -1,6 +1,8 @@
package jobs
import (
+ "net"
+ "net/rpc"
"os"
"os/signal"
"sync"
@@ -8,15 +10,22 @@ import (
"testing"
"time"
+ "github.com/golang/mock/gomock"
endure "github.com/spiral/endure/pkg/container"
+ goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc"
"github.com/spiral/roadrunner/v2/plugins/config"
+ "github.com/spiral/roadrunner/v2/plugins/informer"
"github.com/spiral/roadrunner/v2/plugins/jobs"
"github.com/spiral/roadrunner/v2/plugins/jobs/brokers/amqp"
"github.com/spiral/roadrunner/v2/plugins/jobs/brokers/ephemeral"
"github.com/spiral/roadrunner/v2/plugins/logger"
+ "github.com/spiral/roadrunner/v2/plugins/resetter"
rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc"
"github.com/spiral/roadrunner/v2/plugins/server"
+ jobsv1beta "github.com/spiral/roadrunner/v2/proto/jobs/v1beta"
+ "github.com/spiral/roadrunner/v2/tests/mocks"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
func TestJobsInit(t *testing.T) {
@@ -28,12 +37,114 @@ func TestJobsInit(t *testing.T) {
Prefix: "rr",
}
+ controller := gomock.NewController(t)
+ mockLogger := mocks.NewMockLogger(controller)
+
+ // general
+ mockLogger.EXPECT().Debug("worker destructed", "pid", gomock.Any()).AnyTimes()
+ mockLogger.EXPECT().Debug("worker constructed", "pid", gomock.Any()).AnyTimes()
+ mockLogger.EXPECT().Debug("Started RPC service", "address", "tcp://127.0.0.1:6001", "services", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Error(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
+
+ mockLogger.EXPECT().Info("driver ready", "pipeline", "test-2", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Info("driver ready", "pipeline", "test-3", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+
+ mockLogger.EXPECT().Info("pipeline started", "pipeline", "test-local-2", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Info("pipeline started", "pipeline", "test-1", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Info("pipeline started", "pipeline", "test-2-amqp", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Info("pipeline started", "pipeline", "test-local", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Info("pipeline started", "pipeline", "test-local-3", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+
+ mockLogger.EXPECT().Info("driver initialized", "driver", "amqp", "start", gomock.Any()).Times(2)
+
+ mockLogger.EXPECT().Warn("pipeline stopped", "pipeline", "test-local-3", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Warn("pipeline stopped", "pipeline", "test-local-2", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Warn("pipeline stopped", "pipeline", "test-1", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Warn("pipeline stopped", "pipeline", "test-2-amqp", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Warn("pipeline stopped", "pipeline", "test-local", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+
+ mockLogger.EXPECT().Info("delivery channel closed, leaving the rabbit listener").Times(2)
+
+ err = cont.RegisterAll(
+ cfg,
+ &server.Plugin{},
+ &rpcPlugin.Plugin{},
+ mockLogger,
+ &jobs.Plugin{},
+ &resetter.Plugin{},
+ &informer.Plugin{},
+ &ephemeral.Plugin{},
+ &amqp.Plugin{},
+ )
+ assert.NoError(t, err)
+
+ err = cont.Init()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ ch, err := cont.Serve()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ sig := make(chan os.Signal, 1)
+ signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
+
+ wg := &sync.WaitGroup{}
+ wg.Add(1)
+
+ stopCh := make(chan struct{}, 1)
+
+ go func() {
+ defer wg.Done()
+ for {
+ select {
+ case e := <-ch:
+ assert.Fail(t, "error", e.Error.Error())
+ err = cont.Stop()
+ if err != nil {
+ assert.FailNow(t, "error", err.Error())
+ }
+ case <-sig:
+ err = cont.Stop()
+ if err != nil {
+ assert.FailNow(t, "error", err.Error())
+ }
+ return
+ case <-stopCh:
+ // timeout
+ err = cont.Stop()
+ if err != nil {
+ assert.FailNow(t, "error", err.Error())
+ }
+ return
+ }
+ }
+ }()
+
+ time.Sleep(time.Second * 3)
+ stopCh <- struct{}{}
+ wg.Wait()
+}
+
+func TestJobsNoAMQPGlobal(t *testing.T) {
+ cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
+ assert.NoError(t, err)
+
+ cfg := &config.Viper{
+ Path: "configs/.rr-jobs-init-no-amqp-global.yaml",
+ Prefix: "rr",
+ }
+
err = cont.RegisterAll(
cfg,
&server.Plugin{},
&rpcPlugin.Plugin{},
&logger.ZapLogger{},
&jobs.Plugin{},
+ &resetter.Plugin{},
+ &informer.Plugin{},
&ephemeral.Plugin{},
&amqp.Plugin{},
)
@@ -44,6 +155,55 @@ func TestJobsInit(t *testing.T) {
t.Fatal(err)
}
+ _, err = cont.Serve()
+ require.Error(t, err)
+}
+
+func TestJobsPauseResume(t *testing.T) {
+ cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
+ assert.NoError(t, err)
+
+ cfg := &config.Viper{
+ Path: "configs/.rr-jobs-pause-resume-ephemeral.yaml",
+ Prefix: "rr",
+ }
+
+ controller := gomock.NewController(t)
+ mockLogger := mocks.NewMockLogger(controller)
+
+ // general
+ mockLogger.EXPECT().Debug("worker destructed", "pid", gomock.Any()).AnyTimes()
+ mockLogger.EXPECT().Debug("worker constructed", "pid", gomock.Any()).AnyTimes()
+ mockLogger.EXPECT().Debug("Started RPC service", "address", "tcp://127.0.0.1:6001", "services", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Error(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
+
+ mockLogger.EXPECT().Info("pipeline started", "pipeline", "test-local-2", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Info("pipeline started", "pipeline", "test-local", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+
+ mockLogger.EXPECT().Warn("pipeline stopped", "pipeline", "test-local-3", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Warn("pipeline stopped", "pipeline", "test-local-2", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+ mockLogger.EXPECT().Warn("pipeline stopped", "pipeline", "test-local", "start", gomock.Any(), "elapsed", gomock.Any()).Times(2)
+
+ mockLogger.EXPECT().Info("pipeline active", "pipeline", "test-local", "start", gomock.Any(), "elapsed", gomock.Any()).Times(1)
+
+ err = cont.RegisterAll(
+ cfg,
+ &server.Plugin{},
+ &rpcPlugin.Plugin{},
+ mockLogger,
+ &jobs.Plugin{},
+ &resetter.Plugin{},
+ &informer.Plugin{},
+ &ephemeral.Plugin{},
+ )
+
+ assert.NoError(t, err)
+
+ err = cont.Init()
+ if err != nil {
+ t.Fatal(err)
+ }
+
ch, err := cont.Serve()
if err != nil {
t.Fatal(err)
@@ -84,9 +244,87 @@ func TestJobsInit(t *testing.T) {
}
}()
- time.Sleep(time.Second * 1200)
+ time.Sleep(time.Second * 3)
- stopCh <- struct{}{}
+ t.Run("ephemeralPause", ephemeralPause)
+ t.Run("pushToDisabledPipe", pushToDisabledPipe("test-local"))
+ t.Run("ephemeralResume", ephemeralResume)
+ t.Run("pushToEnabledPipe", pushToPipe("test-local"))
+
+ time.Sleep(time.Second * 1)
+ stopCh <- struct{}{}
wg.Wait()
}
+
+func ephemeralPause(t *testing.T) {
+ conn, err := net.Dial("tcp", "127.0.0.1:6001")
+ assert.NoError(t, err)
+ client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
+
+ pipe := &jobsv1beta.MaintenanceRequest{Pipelines: make([]string, 1)}
+ pipe.GetPipelines()[0] = "test-local"
+
+ er := &jobsv1beta.Empty{}
+ err = client.Call("jobs.Pause", pipe, er)
+ assert.NoError(t, err)
+}
+
+func ephemeralResume(t *testing.T) {
+ conn, err := net.Dial("tcp", "127.0.0.1:6001")
+ assert.NoError(t, err)
+ client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
+
+ pipe := &jobsv1beta.MaintenanceRequest{Pipelines: make([]string, 1)}
+ pipe.GetPipelines()[0] = "test-local"
+
+ er := &jobsv1beta.Empty{}
+ err = client.Call("jobs.Resume", pipe, er)
+ assert.NoError(t, err)
+}
+
+func pushToDisabledPipe(pipeline string) func(t *testing.T) {
+ return func(t *testing.T) {
+ conn, err := net.Dial("tcp", "127.0.0.1:6001")
+ assert.NoError(t, err)
+ client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
+
+ req := &jobsv1beta.PushRequest{Job: &jobsv1beta.Job{
+ Job: "some/php/namespace",
+ Id: "1",
+ Payload: `{"hello":"world"}`,
+ Headers: nil,
+ Options: &jobsv1beta.Options{
+ Priority: 1,
+ Pipeline: pipeline,
+ },
+ }}
+
+ er := &jobsv1beta.Empty{}
+ err = client.Call("jobs.Push", req, er)
+ assert.Error(t, err)
+ }
+}
+
+func pushToPipe(pipeline string) func(t *testing.T) {
+ return func(t *testing.T) {
+ conn, err := net.Dial("tcp", "127.0.0.1:6001")
+ assert.NoError(t, err)
+ client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
+
+ req := &jobsv1beta.PushRequest{Job: &jobsv1beta.Job{
+ Job: "some/php/namespace",
+ Id: "1",
+ Payload: `{"hello":"world"}`,
+ Headers: nil,
+ Options: &jobsv1beta.Options{
+ Priority: 1,
+ Pipeline: pipeline,
+ },
+ }}
+
+ er := &jobsv1beta.Empty{}
+ err = client.Call("jobs.Push", req, er)
+ assert.NoError(t, err)
+ }
+}