mod_keeper_test.go 1.8 KB

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