alive_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright © 2022 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package services_test
  16. import (
  17. "context"
  18. "time"
  19. "github.com/ipfs/go-log"
  20. . "github.com/onsi/ginkgo/v2"
  21. . "github.com/onsi/gomega"
  22. "github.com/mudler/edgevpn/pkg/blockchain"
  23. "github.com/mudler/edgevpn/pkg/logger"
  24. node "github.com/mudler/edgevpn/pkg/node"
  25. . "github.com/mudler/edgevpn/pkg/services"
  26. )
  27. var _ = Describe("Alive service", func() {
  28. token := node.GenerateNewConnectionData().Base64()
  29. logg := logger.New(log.LevelError)
  30. l := node.Logger(logg)
  31. opts := append(
  32. Alive(5*time.Second, 100*time.Second, 15*time.Minute),
  33. node.WithDiscoveryInterval(10*time.Second),
  34. node.FromBase64(true, true, token),
  35. l)
  36. Context("Aliveness check", func() {
  37. It("detect both nodes alive after a while", func() {
  38. ctx, cancel := context.WithCancel(context.Background())
  39. defer cancel()
  40. e2, _ := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)
  41. e1, _ := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)
  42. e1.Start(ctx)
  43. e2.Start(ctx)
  44. ll, _ := e1.Ledger()
  45. ll.Persist(ctx, 5*time.Second, 100*time.Second, "t", "t", "test")
  46. matches := And(ContainElement(e2.Host().ID().String()),
  47. ContainElement(e1.Host().ID().String()))
  48. index := ll.LastBlock().Index
  49. Eventually(func() []string {
  50. ll, err := e1.Ledger()
  51. if err != nil {
  52. return []string{}
  53. }
  54. return AvailableNodes(ll, 15*time.Minute)
  55. }, 100*time.Second, 1*time.Second).Should(matches)
  56. Expect(ll.LastBlock().Index).ToNot(Equal(index))
  57. })
  58. })
  59. Context("Aliveness Scrub", func() {
  60. BeforeEach(func() {
  61. opts = append(
  62. Alive(5*time.Second, 20*time.Second, 15*time.Minute),
  63. node.WithDiscoveryInterval(10*time.Second),
  64. node.FromBase64(true, true, token),
  65. l)
  66. })
  67. It("cleans up after a while", func() {
  68. ctx, cancel := context.WithCancel(context.Background())
  69. defer cancel()
  70. e2, _ := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)
  71. e1, _ := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)
  72. e1.Start(ctx)
  73. e2.Start(ctx)
  74. ll, _ := e1.Ledger()
  75. ll.Persist(ctx, 5*time.Second, 100*time.Second, "t", "t", "test")
  76. matches := And(ContainElement(e2.Host().ID().String()),
  77. ContainElement(e1.Host().ID().String()))
  78. index := ll.LastBlock().Index
  79. Eventually(func() []string {
  80. ll, err := e1.Ledger()
  81. if err != nil {
  82. return []string{}
  83. }
  84. return AvailableNodes(ll, 15*time.Minute)
  85. }, 120*time.Second, 1*time.Second).Should(matches)
  86. Expect(ll.LastBlock().Index).ToNot(Equal(index))
  87. index = ll.LastBlock().Index
  88. Eventually(func() []string {
  89. ll, err := e1.Ledger()
  90. if err != nil {
  91. return []string{}
  92. }
  93. return AvailableNodes(ll, 15*time.Minute)
  94. }, 120*time.Second, 1*time.Second).Should(BeEmpty())
  95. Expect(ll.LastBlock().Index).ToNot(Equal(index))
  96. index = ll.LastBlock().Index
  97. Eventually(func() []string {
  98. ll, err := e1.Ledger()
  99. if err != nil {
  100. return []string{}
  101. }
  102. return AvailableNodes(ll, 15*time.Minute)
  103. }, 60*time.Second, 1*time.Second).Should(matches)
  104. Expect(ll.LastBlock().Index).ToNot(Equal(index))
  105. })
  106. })
  107. })