mod_serv_http_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package mod_serv_http
  2. import (
  3. "os"
  4. "testing"
  5. "time"
  6. "gitp78su.ipnodns.ru/svi/kern/v3/krn/kctx"
  7. . "gitp78su.ipnodns.ru/svi/kern/v3/krn/ktypes"
  8. "gitp78su.ipnodns.ru/svi/kern/v3/mock/mock_env"
  9. )
  10. const (
  11. baseUrl = "http://localhost:18390/"
  12. pathStore = "/store/store_mod_serv_http"
  13. )
  14. type tester struct {
  15. t *testing.T
  16. me *mock_env.MockEnv
  17. ctx IKernelCtx
  18. mod IKernelModule
  19. }
  20. func TestModServHttp(t *testing.T) {
  21. sf := &tester{
  22. t: t,
  23. me: mock_env.MakeEnv(),
  24. ctx: kctx.GetKernelCtx(),
  25. }
  26. _ = os.Unsetenv("LOCAL_STORE_PATH")
  27. _ = os.Setenv("LOCAL_STORE_PATH", pathStore)
  28. _ = os.Unsetenv("LOCAL_HTTP_URL")
  29. os.Setenv("LOCAL_HTTP_URL", baseUrl)
  30. fnClear := func() {
  31. pwd := sf.me.Pwd() + pathStore
  32. _ = os.RemoveAll(pwd)
  33. }
  34. fnClear()
  35. defer fnClear()
  36. sf.ctx.Set("monolitName", "test_monolit", "test")
  37. sf.new()
  38. sf.done()
  39. }
  40. // Завершение работы
  41. func (sf *tester) done() {
  42. sf.t.Log("done")
  43. sf.ctx.Cancel()
  44. sf.ctx.Wg().Wait()
  45. if isWork := sf.mod.IsWork(); isWork {
  46. sf.t.Fatalf("newGood1(): isWork==true")
  47. }
  48. }
  49. // Создание нового модуля HTTP-сервера
  50. func (sf *tester) new() {
  51. sf.t.Log("new")
  52. // sf.newBad1()
  53. sf.newGood1()
  54. }
  55. func (sf *tester) newGood1() {
  56. sf.t.Log("newGood1")
  57. _ = mock_env.MakeEnv()
  58. _ = os.Unsetenv("LOCAL_HTTP_URL")
  59. os.Setenv("LOCAL_HTTP_URL", "http://localhost:18301/")
  60. sf.mod = GetModuleServHttp()
  61. _ = GetModuleServHttp()
  62. if sf.mod == nil {
  63. sf.t.Fatalf("newGood1(): mod==nil")
  64. }
  65. go sf.mod.Run()
  66. for {
  67. time.Sleep(time.Millisecond * 1)
  68. if sf.mod.IsWork() {
  69. return
  70. }
  71. }
  72. }
  73. // нет ничего для создания модуля
  74. func (sf *tester) newBad1() {
  75. sf.t.Log("newBad1")
  76. defer func() {
  77. if _panic := recover(); _panic == nil {
  78. sf.t.Fatalf("newBad1(): panic==nil")
  79. }
  80. }()
  81. _ = GetModuleServHttp()
  82. }