Skip to content

Commit

Permalink
Trim extra non-ascii characters that can arise from manually editing …
Browse files Browse the repository at this point in the history
…sequence number files
  • Loading branch information
adam-talos committed Sep 30, 2021
1 parent 8c26237 commit 44584a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -178,15 +179,15 @@ func (store *fileStore) populateCache() (creationTimePopulated bool, err error)
}

if senderSeqNumBytes, err := ioutil.ReadFile(store.senderSeqNumsFname); err == nil {
if senderSeqNum, err := strconv.Atoi(string(senderSeqNumBytes)); err == nil {
if senderSeqNum, err := strconv.Atoi(strings.Trim(string(senderSeqNumBytes), "\r\n")); err == nil {
if err = store.cache.SetNextSenderMsgSeqNum(senderSeqNum); err != nil {
return creationTimePopulated, errors.Wrap(err, "cache set next sender")
}
}
}

if targetSeqNumBytes, err := ioutil.ReadFile(store.targetSeqNumsFname); err == nil {
if targetSeqNum, err := strconv.Atoi(string(targetSeqNumBytes)); err == nil {
if targetSeqNum, err := strconv.Atoi(strings.Trim(string(targetSeqNumBytes),"\r\n")); err == nil {
if err = store.cache.SetNextTargetMsgSeqNum(targetSeqNum); err != nil {
return creationTimePopulated, errors.Wrap(err, "cache set next target")
}
Expand Down
13 changes: 13 additions & 0 deletions filestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"fmt"
"os"
"path"
"strconv"
"strings"
"testing"
"time"

assert2 "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
Expand Down Expand Up @@ -47,3 +49,14 @@ func (suite *FileStoreTestSuite) TearDownTest() {
func TestFileStoreTestSuite(t *testing.T) {
suite.Run(t, new(FileStoreTestSuite))
}

func TestStringParse(t *testing.T) {
assert := assert2.New(t)
i, err := strconv.Atoi(strings.Trim("00005\n", "\r\n"))
assert.Nil(err)
assert.Equal(5, i)

i, err = strconv.Atoi(strings.Trim("00006\r", "\r\n"))
assert.Nil(err)
assert.Equal(6, i)
}

0 comments on commit 44584a9

Please sign in to comment.