Skip to content

Commit

Permalink
Back-fill tests for externalbuilder.Duration
Browse files Browse the repository at this point in the history
- Use type instead of boxing time.Duration
- Restructure round-trip marshalling tests to table
- Backfill tests to verify behavior of numeric durations decoding

Signed-off-by: Matthew Sykes <matthew.sykes@gmail.com>
  • Loading branch information
sykesm authored and mastersingh24 committed Mar 11, 2021
1 parent 796f760 commit 8843223
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
22 changes: 10 additions & 12 deletions core/container/externalbuilder/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ type Instance struct {
}

// Duration used for the DialTimeout property
type Duration struct {
time.Duration
}
type Duration time.Duration

func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Seconds())
return json.Marshal(time.Duration(d).String())
}

func (d *Duration) UnmarshalJSON(b []byte) error {
Expand All @@ -51,17 +49,18 @@ func (d *Duration) UnmarshalJSON(b []byte) error {

switch value := v.(type) {
case float64:
d.Duration = time.Duration(value)
return nil
*d = Duration(time.Duration(value))
case string:
var err error
if d.Duration, err = time.ParseDuration(value); err != nil {
dur, err := time.ParseDuration(value)
if err != nil {
return err
}
return nil
*d = Duration(dur)
default:
return errors.New("invalid duration")
}

return nil
}

// ChaincodeServerUserData holds "connection.json" information
Expand All @@ -82,10 +81,9 @@ func (c *ChaincodeServerUserData) ChaincodeServerInfo(cryptoDir string) (*ccintf
}
connInfo := &ccintf.ChaincodeServerInfo{Address: c.Address}

if c.DialTimeout == (Duration{}) {
connInfo.ClientConfig.Timeout = time.Duration(c.DialTimeout)
if connInfo.ClientConfig.Timeout == 0 {
connInfo.ClientConfig.Timeout = DialTimeout
} else {
connInfo.ClientConfig.Timeout = c.DialTimeout.Duration
}

// we can expose this if necessary
Expand Down
42 changes: 38 additions & 4 deletions core/container/externalbuilder/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ SPDX-License-Identifier: Apache-2.0
package externalbuilder_test

import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/onsi/gomega/types"

"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/container/ccintf"
"github.com/hyperledger/fabric/core/container/externalbuilder"
"github.com/hyperledger/fabric/internal/pkg/comm"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var _ = Describe("Instance", func() {
Expand Down Expand Up @@ -125,7 +128,7 @@ var _ = Describe("Instance", func() {

ccuserdata = &externalbuilder.ChaincodeServerUserData{
Address: "ccaddress:12345",
DialTimeout: externalbuilder.Duration{10 * time.Second},
DialTimeout: externalbuilder.Duration(10 * time.Second),
TLSRequired: true,
ClientAuthRequired: true,
ClientKey: "fake-key",
Expand Down Expand Up @@ -177,7 +180,7 @@ var _ = Describe("Instance", func() {

Context("dial timeout not provided", func() {
It("returns default dial timeout without dialtimeout", func() {
ccuserdata.DialTimeout = externalbuilder.Duration{}
ccuserdata.DialTimeout = 0

ccinfo, err := ccuserdata.ChaincodeServerInfo(releaseDir)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -236,6 +239,37 @@ var _ = Describe("Instance", func() {
})
})

Describe("Duration", func() {
DescribeTable("Unmarshal",
func(input string, expected externalbuilder.Duration, errMatcher types.GomegaMatcher) {
var d externalbuilder.Duration
err := json.Unmarshal([]byte(input), &d)
Expect(err).To(errMatcher)
},
Entry("Number", `100`, externalbuilder.Duration(100), BeNil()),
Entry("Duration", `"1s"`, externalbuilder.Duration(time.Second), BeNil()),
Entry("List", `[1, 2, 3]`, externalbuilder.Duration(time.Second), MatchError("invalid duration")),
Entry("Nonsense", `"nonsense"`, externalbuilder.Duration(time.Second), MatchError(MatchRegexp(`time: invalid duration "?nonsense"?`))),
)

DescribeTable("Round Trip",
func(d time.Duration) {
marshalled, err := json.Marshal(externalbuilder.Duration(d))
Expect(err).NotTo(HaveOccurred())

var unmarshalled externalbuilder.Duration
err = json.Unmarshal(marshalled, &unmarshalled)
Expect(err).NotTo(HaveOccurred())

Expect(unmarshalled).To(Equal(externalbuilder.Duration(d)))
},
Entry("10ms", 10*time.Millisecond),
Entry("10s", 10*time.Second),
Entry("10m", 10*time.Minute),
Entry("10h", 10*time.Hour),
)
})

Describe("Start", func() {
It("invokes the builder's run command and sets the run status", func() {
err := instance.Start(&ccintf.PeerConnection{
Expand Down

0 comments on commit 8843223

Please sign in to comment.