2022-05-18 16:12:54 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2022 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2022-04-28 19:21:09 +00:00
|
|
|
//go:build !race
|
|
|
|
// +build !race
|
|
|
|
|
2022-04-28 18:50:29 +00:00
|
|
|
package shutdown_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-06 15:03:15 +00:00
|
|
|
"os"
|
2022-04-28 18:50:29 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/r3labs/sse"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/internal/boot"
|
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/internal/shutdown"
|
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/beaconclient"
|
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql"
|
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/gracefulshutdown"
|
|
|
|
)
|
|
|
|
|
2022-06-03 16:47:13 +00:00
|
|
|
var (
|
|
|
|
dbAddress string = "localhost"
|
|
|
|
dbPort int = 8076
|
|
|
|
dbName string = "vulcanize_testing"
|
|
|
|
dbUsername string = "vdbm"
|
|
|
|
dbPassword string = "password"
|
|
|
|
dbDriver string = "PGX"
|
|
|
|
bcAddress string = "localhost"
|
|
|
|
bcPort int = 5052
|
|
|
|
bcConnectionProtocol string = "http"
|
|
|
|
bcType string = "lighthouse"
|
|
|
|
bcBootRetryInterval int = 1
|
|
|
|
bcBootMaxRetry int = 5
|
|
|
|
bcKgTableIncrement int = 10
|
|
|
|
bcUniqueIdentifier int = 100
|
2022-06-06 13:02:43 +00:00
|
|
|
bcCheckDb bool = false
|
2022-06-03 16:47:13 +00:00
|
|
|
maxWaitSecondsShutdown time.Duration = time.Duration(1) * time.Second
|
|
|
|
DB sql.Database
|
|
|
|
BC *beaconclient.BeaconClient
|
|
|
|
err error
|
|
|
|
ctx context.Context
|
|
|
|
notifierCh chan os.Signal
|
|
|
|
)
|
|
|
|
|
2022-04-28 18:50:29 +00:00
|
|
|
var _ = Describe("Shutdown", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
ctx = context.Background()
|
2022-05-24 20:18:55 +00:00
|
|
|
BC, DB, err = boot.BootApplicationWithRetry(ctx, dbAddress, dbPort, dbName, dbUsername, dbPassword, dbDriver, bcAddress,
|
2022-06-06 13:02:43 +00:00
|
|
|
bcPort, bcConnectionProtocol, bcType, bcBootRetryInterval, bcBootMaxRetry, bcKgTableIncrement, "head", true, bcUniqueIdentifier, bcCheckDb)
|
2022-05-06 15:03:15 +00:00
|
|
|
notifierCh = make(chan os.Signal, 1)
|
2022-04-28 18:50:29 +00:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
})
|
|
|
|
|
2022-06-03 16:47:13 +00:00
|
|
|
Describe("Run Shutdown Function for head tracking,", Label("integration"), func() {
|
2022-04-28 18:50:29 +00:00
|
|
|
Context("When Channels are empty,", func() {
|
|
|
|
It("Should Shutdown Successfully.", func() {
|
|
|
|
go func() {
|
2022-06-08 14:26:27 +00:00
|
|
|
_, cancel := context.WithCancel(context.Background())
|
2022-04-28 18:50:29 +00:00
|
|
|
log.Debug("Starting shutdown chan")
|
2022-06-08 14:26:27 +00:00
|
|
|
err = shutdown.ShutdownHeadTracking(ctx, cancel, notifierCh, maxWaitSecondsShutdown, DB, BC)
|
2022-04-28 18:50:29 +00:00
|
|
|
log.Debug("We have completed the shutdown...")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
}()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
Context("When the Channels are not empty,", func() {
|
|
|
|
It("Should try to clear them and shutdown gracefully.", func() {
|
|
|
|
shutdownCh := make(chan bool)
|
|
|
|
//log.SetLevel(log.DebugLevel)
|
|
|
|
go func() {
|
2022-06-08 14:26:27 +00:00
|
|
|
_, cancel := context.WithCancel(context.Background())
|
2022-04-28 18:50:29 +00:00
|
|
|
log.Debug("Starting shutdown chan")
|
2022-06-08 14:26:27 +00:00
|
|
|
err = shutdown.ShutdownHeadTracking(ctx, cancel, notifierCh, maxWaitSecondsShutdown, DB, BC)
|
2022-04-28 18:50:29 +00:00
|
|
|
log.Debug("We have completed the shutdown...")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
shutdownCh <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
messageAddCh := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
log.Debug("Adding messages to Channels")
|
|
|
|
BC.HeadTracking.MessagesCh <- &sse.Event{}
|
2022-05-06 15:03:15 +00:00
|
|
|
//BC.FinalizationTracking.MessagesCh <- &sse.Event{}
|
2022-04-28 18:50:29 +00:00
|
|
|
BC.ReOrgTracking.MessagesCh <- &sse.Event{}
|
|
|
|
log.Debug("Message adding complete")
|
|
|
|
messageAddCh <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-messageAddCh
|
2022-05-06 15:03:15 +00:00
|
|
|
log.Debug("Calling SIGTERM")
|
|
|
|
notifierCh <- syscall.SIGTERM
|
2022-04-28 18:50:29 +00:00
|
|
|
log.Debug("Reading messages from channel")
|
|
|
|
<-BC.HeadTracking.MessagesCh
|
2022-05-06 15:03:15 +00:00
|
|
|
//<-BC.FinalizationTracking.MessagesCh
|
2022-04-28 18:50:29 +00:00
|
|
|
<-BC.ReOrgTracking.MessagesCh
|
|
|
|
}()
|
|
|
|
<-shutdownCh
|
|
|
|
|
|
|
|
})
|
|
|
|
It("Should try to clear them, if it can't, shutdown within a given time frame.", func() {
|
|
|
|
shutdownCh := make(chan bool)
|
|
|
|
//log.SetLevel(log.DebugLevel)
|
|
|
|
go func() {
|
|
|
|
log.Debug("Starting shutdown chan")
|
2022-06-08 14:26:27 +00:00
|
|
|
_, cancel := context.WithCancel(context.Background())
|
|
|
|
err = shutdown.ShutdownHeadTracking(ctx, cancel, notifierCh, maxWaitSecondsShutdown, DB, BC)
|
2022-04-28 18:50:29 +00:00
|
|
|
log.Debug("We have completed the shutdown...")
|
|
|
|
Expect(err).To(MatchError(gracefulshutdown.TimeoutErr(maxWaitSecondsShutdown.String())))
|
|
|
|
shutdownCh <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
log.Debug("Adding messages to Channels")
|
|
|
|
BC.HeadTracking.MessagesCh <- &sse.Event{}
|
2022-05-06 15:03:15 +00:00
|
|
|
//BC.FinalizationTracking.MessagesCh <- &sse.Event{}
|
2022-04-28 18:50:29 +00:00
|
|
|
BC.ReOrgTracking.MessagesCh <- &sse.Event{}
|
|
|
|
log.Debug("Message adding complete")
|
|
|
|
log.Debug("Calling SIGHUP")
|
2022-05-06 15:03:15 +00:00
|
|
|
notifierCh <- syscall.SIGTERM
|
2022-04-28 18:50:29 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
<-shutdownCh
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|