dao_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "flag"
  6. //"fmt"
  7. "os"
  8. "testing"
  9. . "github.com/smartystreets/goconvey/convey"
  10. "go-common/app/service/live/dao-anchor/conf"
  11. "go-common/library/log"
  12. "go-common/library/queue/databus"
  13. )
  14. var (
  15. d *Dao
  16. )
  17. func TestMain(m *testing.M) {
  18. // TODO: other environments?
  19. flag.Set("conf", "../cmd/test.toml")
  20. flag.Parse()
  21. if err := conf.Init(); err != nil {
  22. panic(err)
  23. }
  24. d = New(conf.Conf)
  25. m.Run()
  26. os.Exit(0)
  27. }
  28. func TestCanConsume(t *testing.T) {
  29. flag.Set("conf", "../cmd/test.toml")
  30. flag.Parse()
  31. if err := conf.Init(); err != nil {
  32. panic(err)
  33. }
  34. log.Init(conf.Conf.Log)
  35. Convey("", t, func(c C) {
  36. ctx := context.TODO()
  37. d := New(conf.Conf)
  38. msg := &databus.Message{
  39. Topic: "test-topic",
  40. Value: json.RawMessage(`{"msg_id":"test-msg-id", "other_key":"value"}`),
  41. }
  42. d.clearConsumed(ctx, msg)
  43. can := d.CanConsume(ctx, msg)
  44. So(can, ShouldBeTrue)
  45. can = d.CanConsume(ctx, msg)
  46. So(can, ShouldBeFalse)
  47. d.clearConsumed(ctx, msg)
  48. can = d.CanConsume(ctx, msg)
  49. So(can, ShouldBeTrue)
  50. })
  51. }