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 authored and ackleymi committed May 8, 2023
1 parent 199c332 commit 4883356
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 @@ -22,6 +22,7 @@ import (
"os"
"path"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -213,15 +214,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 @@ -19,10 +19,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 @@ -62,3 +64,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 4883356

Please sign in to comment.