kmodule_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package kmodule
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "gitp78su.ipnodns.ru/svi/kern/krn/kctx"
  7. . "gitp78su.ipnodns.ru/svi/kern/krn/ktypes"
  8. )
  9. type tester struct {
  10. t *testing.T
  11. mod IKernelModule
  12. }
  13. func TestKernelModule(t *testing.T) {
  14. sf := &tester{
  15. t: t,
  16. }
  17. sf.new()
  18. sf.run()
  19. sf.isWork()
  20. sf.done()
  21. sf.recErr()
  22. }
  23. // Регистрация ошибки
  24. func (sf *tester) recErr() {
  25. sf.t.Log("recErr")
  26. mod := sf.mod.(*kModule)
  27. err := fmt.Errorf("tra-la-la")
  28. mod.recErr(err)
  29. if live := sf.mod.Live(); live == "" {
  30. sf.t.Fatalf("recErr(): live empty")
  31. }
  32. if stat := sf.mod.Stat(); stat == nil {
  33. sf.t.Fatalf("recErr(): stat==nil")
  34. }
  35. }
  36. // Работа после остановки локальной шины
  37. func (sf *tester) done() {
  38. sf.t.Log("done")
  39. kCtx := kctx.GetKernelCtx()
  40. time.Sleep(time.Millisecond * 250)
  41. kCtx.Cancel()
  42. kCtx.Done()
  43. time.Sleep(time.Millisecond * 200)
  44. }
  45. // Проверить признак работы
  46. func (sf *tester) isWork() {
  47. sf.t.Log("isWork")
  48. defer func() {
  49. if _panic := recover(); _panic == nil {
  50. sf.t.Fatalf("isWork(): panic==nil")
  51. }
  52. }()
  53. _ = sf.mod.IsWork()
  54. }
  55. // Запускает модуль в работу
  56. func (sf *tester) run() {
  57. sf.t.Log("run")
  58. defer func() {
  59. if _panic := recover(); _panic == nil {
  60. sf.t.Fatalf("run(): panic==nil")
  61. }
  62. }()
  63. mod := sf.mod.(*kModule)
  64. mod.timePhase.Set(5) // Настройка переменной модуля
  65. sf.mod.Run()
  66. }
  67. // Создание нового модуля ядра
  68. func (sf *tester) new() {
  69. sf.t.Log("new")
  70. sf.newBad1()
  71. sf.newGood1()
  72. }
  73. func (sf *tester) newGood1() {
  74. sf.t.Log("newGood1")
  75. sf.mod = NewKernelModule("test_module")
  76. if name := sf.mod.Name(); name != "test_module" {
  77. sf.t.Fatalf("newGood1(): name(%v)!='test_module'", name)
  78. }
  79. if ctx := sf.mod.Ctx(); ctx == nil {
  80. sf.t.Fatalf("newGood1(): ctx==nil")
  81. }
  82. if _log := sf.mod.Log(); _log == nil {
  83. sf.t.Fatalf("newGood1(): log==nil")
  84. }
  85. }
  86. // Нет имени модуля
  87. func (sf *tester) newBad1() {
  88. sf.t.Log("newBad1")
  89. defer func() {
  90. if _panic := recover(); _panic == nil {
  91. sf.t.Fatalf("newBad1(): panic==nil")
  92. }
  93. }()
  94. _ = NewKernelModule("")
  95. }