2020-07-14 18:11:27 +02:00
|
|
|
// Copyright (c) Mainflux
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package nats_test
|
|
|
|
|
|
|
|
import (
|
2023-10-18 18:00:38 +03:00
|
|
|
"context"
|
2020-07-14 18:11:27 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
mflog "github.com/mainflux/mainflux/logger"
|
2020-07-14 18:11:27 +02:00
|
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
|
|
|
"github.com/mainflux/mainflux/pkg/messaging/nats"
|
2023-08-11 02:30:25 -07:00
|
|
|
"github.com/ory/dockertest/v3"
|
2020-07-14 18:11:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
publisher messaging.Publisher
|
|
|
|
pubsub messaging.PubSub
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-10-18 18:00:38 +03:00
|
|
|
container, err := pool.RunWithOptions(&dockertest.RunOptions{
|
|
|
|
Repository: "nats",
|
|
|
|
Tag: "2.9.21-alpine",
|
|
|
|
Cmd: []string{"-DVV", "-js"},
|
|
|
|
})
|
2020-07-14 18:11:27 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not start container: %s", err)
|
|
|
|
}
|
|
|
|
handleInterrupt(pool, container)
|
|
|
|
|
2023-10-24 18:22:53 +03:00
|
|
|
address := fmt.Sprintf("nats://%s:%s", "localhost", container.GetPort("4222/tcp"))
|
2020-07-14 18:11:27 +02:00
|
|
|
if err := pool.Retry(func() error {
|
2023-10-18 18:00:38 +03:00
|
|
|
publisher, err = nats.NewPublisher(context.Background(), address)
|
2020-07-14 18:11:27 +02:00
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
log.Fatalf("Could not connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
logger, err := mflog.New(os.Stdout, "error")
|
2020-07-14 18:11:27 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
if err := pool.Retry(func() error {
|
2023-10-18 18:00:38 +03:00
|
|
|
pubsub, err = nats.NewPubSub(context.Background(), address, logger)
|
2020-07-14 18:11:27 +02:00
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
log.Fatalf("Could not connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
if err := pool.Purge(container); err != nil {
|
|
|
|
log.Fatalf("Could not purge container: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleInterrupt(pool *dockertest.Pool, container *dockertest.Resource) {
|
2022-06-09 21:57:37 +02:00
|
|
|
c := make(chan os.Signal, 2)
|
2020-07-14 18:11:27 +02:00
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
NOISSUE - Update pubsub tests for rabbitmq (#1656)
* testPubSub failing on channel
Signed-off-by: aryan <aryangodara03@gmail.com>
* testPubSub failing, rest passing
Signed-off-by: aryan <aryangodara03@gmail.com>
* all tests passing
Signed-off-by: aryan <aryangodara03@gmail.com>
* removed unnecessary lines
Signed-off-by: aryan <aryangodara03@gmail.com>
* rename subunsub to unsubscribe
Signed-off-by: aryan <aryangodara03@gmail.com>
* nats tests working, rabbitmq left
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix TestPublisher
Signed-off-by: aryan <aryangodara03@gmail.com>
* changed clientID of pubsub
Signed-off-by: aryan <aryangodara03@gmail.com>
* testSubscribe failing, rest all passing
Signed-off-by: aryan <aryangodara03@gmail.com>
* testPubsub failing
Signed-off-by: aryan <aryangodara03@gmail.com>
* TestSubscribe(s) failing, rest tests passing.
Signed-off-by: aryan <aryangodara03@gmail.com>
* For Rabbitmq, all tests are passing.
Signed-off-by: aryan <aryangodara03@gmail.com>
* For Nats, All tests passing, but unreliable
Signed-off-by: aryan <aryangodara03@gmail.com>
* Fix typos and improve variable names.
Signed-off-by: aryan <aryangodara03@gmail.com>
* nats tests passing, rabbitmq half tests passing.
Signed-off-by: aryan <aryangodara03@gmail.com>
* all tests working.
Signed-off-by: aryan <aryangodara03@gmail.com>
* removed commented out, dead code.
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix errors, queuesubscribe tests for nats.
Signed-off-by: aryan <aryangodara03@gmail.com>
* updated broker.Connect to opts.Connect for nats.
Signed-off-by: aryan <aryangodara03@gmail.com>
* experimental soluton
Signed-off-by: aryan <aryangodara03@gmail.com>
* Revert nats back to original status.
Signed-off-by: aryan <aryangodara03@gmail.com>
* Remove unnecessary commit
Signed-off-by: aryan <aryangodara03@gmail.com>
Signed-off-by: aryan <aryangodara03@gmail.com>
2022-12-31 06:48:31 +05:30
|
|
|
|
2020-07-14 18:11:27 +02:00
|
|
|
go func() {
|
|
|
|
<-c
|
|
|
|
if err := pool.Purge(container); err != nil {
|
|
|
|
log.Fatalf("Could not purge container: %s", err)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
}
|