gateway_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package backends
  2. import (
  3. "fmt"
  4. "github.com/flashmob/go-guerrilla/log"
  5. "github.com/flashmob/go-guerrilla/mail"
  6. "strings"
  7. "testing"
  8. "time"
  9. )
  10. func TestStates(t *testing.T) {
  11. gw := BackendGateway{}
  12. str := fmt.Sprintf("%s", gw.State)
  13. if strings.Index(str, "NewState") != 0 {
  14. t.Error("Backend should begin in NewState")
  15. }
  16. }
  17. func TestInitialize(t *testing.T) {
  18. c := BackendConfig{
  19. "save_process": "HeadersParser|Debugger",
  20. "log_received_mails": true,
  21. "save_workers_size": "1",
  22. }
  23. gateway := &BackendGateway{}
  24. err := gateway.Initialize(c)
  25. if err != nil {
  26. t.Error("Gateway did not init because:", err)
  27. t.Fail()
  28. }
  29. if gateway.processors == nil {
  30. t.Error("gateway.chains should not be nil")
  31. } else if len(gateway.processors) != 1 {
  32. t.Error("len(gateway.chains) should be 1, but got", len(gateway.processors))
  33. }
  34. if gateway.conveyor == nil {
  35. t.Error("gateway.conveyor should not be nil")
  36. } else if cap(gateway.conveyor) != gateway.workersSize() {
  37. t.Error("gateway.conveyor channel buffer cap does not match worker size, cap was", cap(gateway.conveyor))
  38. }
  39. if gateway.State != BackendStateInitialized {
  40. t.Error("gateway.State is not in initialized state, got ", gateway.State)
  41. }
  42. }
  43. func TestStartProcessStop(t *testing.T) {
  44. c := BackendConfig{
  45. "save_process": "HeadersParser|Debugger",
  46. "log_received_mails": true,
  47. "save_workers_size": 2,
  48. }
  49. gateway := &BackendGateway{}
  50. err := gateway.Initialize(c)
  51. mainlog, _ := log.GetLogger(log.OutputOff.String(), "debug")
  52. Svc.SetMainlog(mainlog)
  53. if err != nil {
  54. t.Error("Gateway did not init because:", err)
  55. t.Fail()
  56. }
  57. err = gateway.Start()
  58. if err != nil {
  59. t.Error("Gateway did not start because:", err)
  60. t.Fail()
  61. }
  62. if gateway.State != BackendStateRunning {
  63. t.Error("gateway.State is not in rinning state, got ", gateway.State)
  64. }
  65. // can we place an envelope on the conveyor channel?
  66. e := &mail.Envelope{
  67. RemoteIP: "127.0.0.1",
  68. QueuedId: "abc12345",
  69. Helo: "helo.example.com",
  70. MailFrom: mail.Address{User: "test", Host: "example.com"},
  71. TLS: true,
  72. }
  73. e.PushRcpt(mail.Address{User: "test", Host: "example.com"})
  74. e.Data.WriteString("Subject:Test\n\nThis is a test.")
  75. notify := make(chan *notifyMsg)
  76. gateway.conveyor <- &workerMsg{e, notify, TaskSaveMail}
  77. // it should not produce any errors
  78. // headers (subject) should be parsed.
  79. select {
  80. case status := <-notify:
  81. if status.err != nil {
  82. t.Error("envelope processing failed with:", status.err)
  83. }
  84. if e.Header["Subject"][0] != "Test" {
  85. t.Error("envelope processing did not parse header")
  86. }
  87. case <-time.After(time.Second):
  88. t.Error("gateway did not respond after 1 second")
  89. t.Fail()
  90. }
  91. err = gateway.Shutdown()
  92. if err != nil {
  93. t.Error("Gateway did not shutdown")
  94. }
  95. }