2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
2019-01-25 08:59:23 +00:00
|
|
|
package watcher_test
|
2018-02-13 16:31:57 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-19 01:55:15 +00:00
|
|
|
"errors"
|
2019-09-30 17:37:16 +00:00
|
|
|
|
2018-02-13 16:31:57 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2019-01-31 23:14:42 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/constants"
|
2019-09-19 03:17:02 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/logs"
|
2019-01-31 23:14:42 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/mocks"
|
2019-01-25 08:59:23 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/watcher"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/fakes"
|
2018-02-13 16:31:57 +00:00
|
|
|
)
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
var errExecuteClosed = errors.New("this error means the mocks were finished executing")
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
var _ = Describe("Event Watcher", func() {
|
|
|
|
var (
|
|
|
|
delegator *mocks.MockLogDelegator
|
|
|
|
extractor *mocks.MockLogExtractor
|
2019-08-12 17:26:49 +00:00
|
|
|
eventWatcher *watcher.EventWatcher
|
2019-08-06 21:57:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
delegator = &mocks.MockLogDelegator{}
|
|
|
|
extractor = &mocks.MockLogExtractor{}
|
2019-08-12 17:26:49 +00:00
|
|
|
eventWatcher = &watcher.EventWatcher{
|
2019-08-06 21:57:08 +00:00
|
|
|
LogDelegator: delegator,
|
|
|
|
LogExtractor: extractor,
|
|
|
|
}
|
2018-02-13 16:31:57 +00:00
|
|
|
})
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
Describe("AddTransformers", func() {
|
2018-12-07 17:10:36 +00:00
|
|
|
var (
|
2019-08-06 21:57:08 +00:00
|
|
|
fakeTransformerOne, fakeTransformerTwo *mocks.MockEventTransformer
|
2018-12-07 17:10:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2019-08-06 21:57:08 +00:00
|
|
|
fakeTransformerOne = &mocks.MockEventTransformer{}
|
|
|
|
fakeTransformerOne.SetTransformerConfig(mocks.FakeTransformerConfig)
|
|
|
|
fakeTransformerTwo = &mocks.MockEventTransformer{}
|
|
|
|
fakeTransformerTwo.SetTransformerConfig(mocks.FakeTransformerConfig)
|
|
|
|
initializers := []transformer.EventTransformerInitializer{
|
|
|
|
fakeTransformerOne.FakeTransformerInitializer,
|
|
|
|
fakeTransformerTwo.FakeTransformerInitializer,
|
|
|
|
}
|
2018-12-11 12:47:04 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := eventWatcher.AddTransformers(initializers)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-12-07 17:10:36 +00:00
|
|
|
})
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
It("adds initialized transformer to log delegator", func() {
|
|
|
|
expectedTransformers := []transformer.EventTransformer{
|
|
|
|
fakeTransformerOne,
|
|
|
|
fakeTransformerTwo,
|
|
|
|
}
|
|
|
|
Expect(delegator.AddedTransformers).To(Equal(expectedTransformers))
|
|
|
|
})
|
2019-03-27 04:05:30 +00:00
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
It("adds transformer config to log extractor", func() {
|
|
|
|
expectedConfigs := []transformer.EventTransformerConfig{
|
|
|
|
mocks.FakeTransformerConfig,
|
|
|
|
mocks.FakeTransformerConfig,
|
|
|
|
}
|
|
|
|
Expect(extractor.AddedConfigs).To(Equal(expectedConfigs))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Execute", func() {
|
2019-08-12 17:26:49 +00:00
|
|
|
|
|
|
|
It("extracts watched logs", func(done Done) {
|
2019-09-19 03:17:02 +00:00
|
|
|
delegator.DelegateErrors = []error{logs.ErrNoLogs}
|
2019-09-19 01:55:15 +00:00
|
|
|
extractor.ExtractLogsErrors = []error{nil, errExecuteClosed}
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := eventWatcher.Execute(constants.HeaderUnchecked)
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
Expect(err).To(MatchError(errExecuteClosed))
|
|
|
|
Eventually(func() bool {
|
|
|
|
return extractor.ExtractLogsCount > 0
|
|
|
|
}).Should(BeTrue())
|
2019-08-12 17:26:49 +00:00
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if extracting logs fails", func(done Done) {
|
2019-09-19 03:17:02 +00:00
|
|
|
delegator.DelegateErrors = []error{logs.ErrNoLogs}
|
2019-08-12 17:26:49 +00:00
|
|
|
extractor.ExtractLogsErrors = []error{fakes.FakeError}
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := eventWatcher.Execute(constants.HeaderUnchecked)
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
2019-08-12 17:26:49 +00:00
|
|
|
close(done)
|
2019-03-27 04:05:30 +00:00
|
|
|
})
|
|
|
|
|
2019-08-12 17:26:49 +00:00
|
|
|
It("extracts watched logs again if missing headers found", func(done Done) {
|
2019-09-19 03:17:02 +00:00
|
|
|
delegator.DelegateErrors = []error{logs.ErrNoLogs}
|
2019-09-19 01:55:15 +00:00
|
|
|
extractor.ExtractLogsErrors = []error{nil, errExecuteClosed}
|
2019-03-27 04:05:30 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := eventWatcher.Execute(constants.HeaderUnchecked)
|
2019-03-27 04:05:30 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
Expect(err).To(MatchError(errExecuteClosed))
|
|
|
|
Eventually(func() bool {
|
|
|
|
return extractor.ExtractLogsCount > 1
|
|
|
|
}).Should(BeTrue())
|
2019-08-12 17:26:49 +00:00
|
|
|
close(done)
|
2019-03-27 04:05:30 +00:00
|
|
|
})
|
|
|
|
|
2019-08-12 17:26:49 +00:00
|
|
|
It("returns error if extracting logs fails on subsequent run", func(done Done) {
|
2019-09-19 03:17:02 +00:00
|
|
|
delegator.DelegateErrors = []error{logs.ErrNoLogs}
|
2019-08-12 17:26:49 +00:00
|
|
|
extractor.ExtractLogsErrors = []error{nil, fakes.FakeError}
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := eventWatcher.Execute(constants.HeaderUnchecked)
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
2019-08-12 17:26:49 +00:00
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
It("delegates untransformed logs", func() {
|
|
|
|
delegator.DelegateErrors = []error{nil, errExecuteClosed}
|
2019-09-19 03:17:02 +00:00
|
|
|
extractor.ExtractLogsErrors = []error{logs.ErrNoUncheckedHeaders}
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := eventWatcher.Execute(constants.HeaderUnchecked)
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
Expect(err).To(MatchError(errExecuteClosed))
|
|
|
|
Eventually(func() bool {
|
|
|
|
return delegator.DelegateCallCount > 0
|
|
|
|
}).Should(BeTrue())
|
2019-08-12 17:26:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if delegating logs fails", func(done Done) {
|
|
|
|
delegator.DelegateErrors = []error{fakes.FakeError}
|
2019-09-19 03:17:02 +00:00
|
|
|
extractor.ExtractLogsErrors = []error{logs.ErrNoUncheckedHeaders}
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := eventWatcher.Execute(constants.HeaderUnchecked)
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
2019-08-12 17:26:49 +00:00
|
|
|
close(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("delegates logs again if untransformed logs found", func(done Done) {
|
2019-09-19 01:55:15 +00:00
|
|
|
delegator.DelegateErrors = []error{nil, nil, nil, errExecuteClosed}
|
2019-09-19 03:17:02 +00:00
|
|
|
extractor.ExtractLogsErrors = []error{logs.ErrNoUncheckedHeaders}
|
2019-08-12 17:26:49 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := eventWatcher.Execute(constants.HeaderUnchecked)
|
2019-07-23 19:57:52 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
Expect(err).To(MatchError(errExecuteClosed))
|
|
|
|
Eventually(func() bool {
|
|
|
|
return delegator.DelegateCallCount > 1
|
|
|
|
}).Should(BeTrue())
|
2019-08-12 17:26:49 +00:00
|
|
|
close(done)
|
2019-07-23 19:57:52 +00:00
|
|
|
})
|
|
|
|
|
2019-08-12 17:26:49 +00:00
|
|
|
It("returns error if delegating logs fails on subsequent run", func(done Done) {
|
|
|
|
delegator.DelegateErrors = []error{nil, fakes.FakeError}
|
2019-09-19 03:17:02 +00:00
|
|
|
extractor.ExtractLogsErrors = []error{logs.ErrNoUncheckedHeaders}
|
2018-12-07 17:10:36 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
err := eventWatcher.Execute(constants.HeaderUnchecked)
|
2018-12-07 17:10:36 +00:00
|
|
|
|
2019-09-19 01:55:15 +00:00
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
2019-08-12 17:26:49 +00:00
|
|
|
close(done)
|
2018-12-10 20:12:19 +00:00
|
|
|
})
|
2018-02-13 16:31:57 +00:00
|
|
|
})
|
|
|
|
})
|