2019-04-26 15:42:40 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 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/>.
|
|
|
|
|
2019-04-24 20:05:57 +00:00
|
|
|
package fetcher_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-09-30 17:37:16 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-04-24 20:05:57 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/hpcloud/tail"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/fetcher"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Csv Tail Storage Fetcher", func() {
|
|
|
|
var (
|
|
|
|
errorsChannel chan error
|
|
|
|
mockTailer *fakes.MockTailer
|
2019-07-29 19:28:20 +00:00
|
|
|
diffsChannel chan utils.StorageDiff
|
2019-04-24 20:05:57 +00:00
|
|
|
storageFetcher fetcher.CsvTailStorageFetcher
|
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
errorsChannel = make(chan error)
|
2019-07-29 19:28:20 +00:00
|
|
|
diffsChannel = make(chan utils.StorageDiff)
|
2019-04-24 20:05:57 +00:00
|
|
|
mockTailer = fakes.NewMockTailer()
|
|
|
|
storageFetcher = fetcher.NewCsvTailStorageFetcher(mockTailer)
|
|
|
|
})
|
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
It("adds error to errors channel if tailing file fails", func(done Done) {
|
2019-04-24 20:05:57 +00:00
|
|
|
mockTailer.TailErr = fakes.FakeError
|
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
go storageFetcher.FetchStorageDiffs(diffsChannel, errorsChannel)
|
2019-04-24 20:05:57 +00:00
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
Expect(<-errorsChannel).To(MatchError(fakes.FakeError))
|
|
|
|
close(done)
|
2019-04-24 20:05:57 +00:00
|
|
|
})
|
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
It("adds parsed csv row to rows channel for storage diff", func(done Done) {
|
2019-04-24 20:05:57 +00:00
|
|
|
line := getFakeLine()
|
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
go storageFetcher.FetchStorageDiffs(diffsChannel, errorsChannel)
|
2019-04-24 20:05:57 +00:00
|
|
|
mockTailer.Lines <- line
|
|
|
|
|
2019-08-16 21:01:01 +00:00
|
|
|
expectedRow, err := utils.FromParityCsvRow(strings.Split(line.Text, ","))
|
2019-04-24 20:05:57 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-07-29 19:28:20 +00:00
|
|
|
Expect(<-diffsChannel).To(Equal(expectedRow))
|
2019-04-25 17:44:14 +00:00
|
|
|
close(done)
|
2019-04-24 20:05:57 +00:00
|
|
|
})
|
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
It("adds error to errors channel if parsing csv fails", func(done Done) {
|
2019-04-24 20:05:57 +00:00
|
|
|
line := &tail.Line{Text: "invalid"}
|
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
go storageFetcher.FetchStorageDiffs(diffsChannel, errorsChannel)
|
2019-04-24 20:05:57 +00:00
|
|
|
mockTailer.Lines <- line
|
|
|
|
|
2019-04-25 17:44:14 +00:00
|
|
|
Expect(<-errorsChannel).To(HaveOccurred())
|
2019-04-26 15:36:42 +00:00
|
|
|
select {
|
2019-07-29 19:28:20 +00:00
|
|
|
case <-diffsChannel:
|
2019-04-26 15:36:42 +00:00
|
|
|
Fail("value passed to rows channel on error")
|
|
|
|
default:
|
|
|
|
Succeed()
|
|
|
|
}
|
2019-04-25 17:44:14 +00:00
|
|
|
close(done)
|
2019-04-24 20:05:57 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
func getFakeLine() *tail.Line {
|
|
|
|
address := common.HexToAddress("0x1234567890abcdef")
|
|
|
|
blockHash := []byte{4, 5, 6}
|
|
|
|
blockHeight := int64(789)
|
|
|
|
storageKey := []byte{9, 8, 7}
|
|
|
|
storageValue := []byte{6, 5, 4}
|
|
|
|
return &tail.Line{
|
|
|
|
Text: fmt.Sprintf("%s,%s,%d,%s,%s", common.Bytes2Hex(address.Bytes()), common.Bytes2Hex(blockHash),
|
|
|
|
blockHeight, common.Bytes2Hex(storageKey), common.Bytes2Hex(storageValue)),
|
|
|
|
Time: time.Time{},
|
|
|
|
Err: nil,
|
|
|
|
}
|
|
|
|
}
|