alive_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package services_test
  14. import (
  15. "context"
  16. "time"
  17. "github.com/ipfs/go-log"
  18. . "github.com/onsi/ginkgo/v2"
  19. . "github.com/onsi/gomega"
  20. "github.com/mudler/edgevpn/pkg/blockchain"
  21. "github.com/mudler/edgevpn/pkg/logger"
  22. node "github.com/mudler/edgevpn/pkg/node"
  23. . "github.com/mudler/edgevpn/pkg/services"
  24. )
  25. var _ = Describe("Alive service", func() {
  26. token := node.GenerateNewConnectionData().Base64()
  27. logg := logger.New(log.LevelError)
  28. l := node.Logger(logg)
  29. opts := append(
  30. Alive(5*time.Second, 100*time.Second, 15*time.Minute),
  31. node.WithDiscoveryInterval(10*time.Second),
  32. node.FromBase64(true, true, token, nil, nil),
  33. l)
  34. Context("Aliveness check", func() {
  35. It("detect both nodes alive after a while", func() {
  36. ctx, cancel := context.WithCancel(context.Background())
  37. defer cancel()
  38. e2, _ := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)
  39. e1, _ := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)
  40. e1.Start(ctx)
  41. e2.Start(ctx)
  42. ll, _ := e1.Ledger()
  43. ll.Persist(ctx, 5*time.Second, 100*time.Second, "t", "t", "test")
  44. matches := And(ContainElement(e2.Host().ID().String()),
  45. ContainElement(e1.Host().ID().String()))
  46. index := ll.LastBlock().Index
  47. Eventually(func() []string {
  48. ll, err := e1.Ledger()
  49. if err != nil {
  50. return []string{}
  51. }
  52. return AvailableNodes(ll, 15*time.Minute)
  53. }, 100*time.Second, 1*time.Second).Should(matches)
  54. Expect(ll.LastBlock().Index).ToNot(Equal(index))
  55. })
  56. })
  57. Context("Aliveness Scrub", func() {
  58. BeforeEach(func() {
  59. opts = append(
  60. Alive(10*time.Second, 30*time.Second, 15*time.Minute),
  61. node.WithDiscoveryInterval(10*time.Second),
  62. node.FromBase64(true, true, token, nil, nil),
  63. l)
  64. })
  65. It("cleans up after a while", func() {
  66. ctx, cancel := context.WithCancel(context.Background())
  67. defer cancel()
  68. e2, _ := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)
  69. e1, _ := node.New(append(opts, node.WithStore(&blockchain.MemoryStore{}))...)
  70. e1.Start(ctx)
  71. time.Sleep(5 * time.Second)
  72. e2.Start(ctx)
  73. ll, _ := e1.Ledger()
  74. ll.Persist(ctx, 5*time.Second, 100*time.Second, "t", "t", "test")
  75. matches := And(ContainElement(e2.Host().ID().String()),
  76. ContainElement(e1.Host().ID().String()))
  77. index := ll.LastBlock().Index
  78. Eventually(func() []string {
  79. ll, err := e1.Ledger()
  80. if err != nil {
  81. return []string{}
  82. }
  83. return AvailableNodes(ll, 15*time.Minute)
  84. }, 120*time.Second, 1*time.Second).Should(matches)
  85. Expect(ll.LastBlock().Index).ToNot(Equal(index))
  86. index = ll.LastBlock().Index
  87. Eventually(func() []string {
  88. ll, err := e1.Ledger()
  89. if err != nil {
  90. return []string{}
  91. }
  92. return AvailableNodes(ll, 15*time.Minute)
  93. }, 360*time.Second, 1*time.Second).Should(BeEmpty())
  94. Expect(ll.LastBlock().Index).ToNot(Equal(index))
  95. index = ll.LastBlock().Index
  96. Eventually(func() []string {
  97. ll, err := e1.Ledger()
  98. if err != nil {
  99. return []string{}
  100. }
  101. return AvailableNodes(ll, 15*time.Minute)
  102. }, 60*time.Second, 1*time.Second).Should(matches)
  103. Expect(ll.LastBlock().Index).ToNot(Equal(index))
  104. })
  105. })
  106. })