• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperledger / fabric-x-committer / 23008022910

12 Mar 2026 02:50PM UTC coverage: 90.6% (+0.9%) from 89.735%
23008022910

Pull #435

github

liran-funaro
Improve test coverage of API and workload

Signed-off-by: Liran Funaro <liran.funaro@gmail.com>
Pull Request #435: Improve test coverage of API and workload

38 of 41 new or added lines in 3 files covered. (92.68%)

8 existing lines in 4 files now uncovered.

9494 of 10479 relevant lines covered (90.6%)

1.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.69
/utils/utils.go
1
/*
2
Copyright IBM Corp. All Rights Reserved.
3

4
SPDX-License-Identifier: Apache-2.0
5
*/
6

7
package utils
8

9
import (
10
        "context"
11
        "encoding/json"
12
        "fmt"
13
        "io"
14
        "os"
15
        "strings"
16

17
        "github.com/cockroachdb/errors"
18
        "google.golang.org/grpc/peer"
19

20
        "github.com/hyperledger/fabric-x-common/api/applicationpb"
21
        "github.com/hyperledger/fabric-x-common/api/committerpb"
22
)
23

24
// ErrActiveStream represents the error when attempting to create a new stream while one is already active.
25
// The system only allows a single active stream at any given time.
26
var ErrActiveStream = errors.New("a stream is already active. Only one active stream is allowed at a time")
27

28
// FileExists returns true if a file path exists.
29
func FileExists(path string) bool {
1✔
30
        _, err := os.Stat(path)
1✔
31
        return !os.IsNotExist(err)
1✔
32
}
1✔
33

34
// LazyJSON will lazily marshal a struct for logging purposes.
35
type LazyJSON struct {
36
        O      any
37
        Indent string
38
}
39

40
// String marshals the give object as JSON.
41
func (lj *LazyJSON) String() string {
1✔
42
        if lj.O == nil {
1✔
43
                return "{}"
×
44
        }
×
45
        var p []byte
1✔
46
        var err error
1✔
47
        if lj.Indent != "" {
2✔
48
                p, err = json.MarshalIndent(lj.O, "", lj.Indent)
1✔
49
        } else {
2✔
50
                p, err = json.Marshal(lj.O)
1✔
51
        }
1✔
52
        if err != nil {
1✔
53
                return fmt.Sprintf("cannot marshal object: %v", err)
×
54
        }
×
55
        return string(p)
1✔
56
}
57

58
// Must panics given an error.
59
func Must(err error, msg ...string) {
2✔
60
        if err != nil {
2✔
61
                panic(errors.Wrap(err, strings.Join(msg, " ")))
×
62
        }
63
}
64

65
// Mustf panics given an error.
66
func Mustf(err error, format string, args ...any) {
1✔
67
        if err != nil {
1✔
NEW
68
                panic(errors.Wrapf(err, format, args...))
×
69
        }
70
}
71

72
// MustRead reads a byte array of the given size from the source.
73
// It panics if the read fails, or cannot read the requested size.
74
// "crypto/rand" and "math/rand" never fail and always returns the correct length.
75
func MustRead(source io.Reader, size int) []byte {
2✔
76
        value := make([]byte, size)
2✔
77
        n, err := source.Read(value)
2✔
78
        Must(err)
2✔
79
        if n != size {
2✔
80
                panic(errors.Errorf("expected size of %d, got %d", size, n))
×
81
        }
82
        return value
2✔
83
}
84

85
// ProcessErr wraps a non-nil error with a message using %w for unwrapping.
86
// Returns nil if the error is nil, otherwise returns the wrapped error.
87
// Example to the call of the function: utils.ProcessErr(g.Wait(), "sidecar has been stopped").
88
func ProcessErr(err error, msg string) error {
2✔
89
        if err != nil {
4✔
90
                return fmt.Errorf("%s: %w", msg, err) //nolint:wrapcheck
2✔
91
        }
2✔
92
        return nil
1✔
93
}
94

95
// CountAppearances returns the number of appearances each item have.
96
func CountAppearances[T comparable](items []T) map[T]int {
2✔
97
        countMap := make(map[T]int)
2✔
98
        for _, item := range items {
4✔
99
                countMap[item]++
2✔
100
        }
2✔
101
        return countMap
2✔
102
}
103

104
// ExtractServerAddress returns the stream's server (local) address.
105
func ExtractServerAddress(ctx context.Context) string {
2✔
106
        p, ok := peer.FromContext(ctx)
2✔
107
        if !ok || p == nil || p.LocalAddr == nil {
2✔
108
                return ""
×
109
        }
×
110
        return p.LocalAddr.String()
2✔
111
}
112

113
// IsConfigTx returns true if the namespaces indicate a config transaction.
114
func IsConfigTx(namespaces []*applicationpb.TxNamespace) bool {
2✔
115
        return len(namespaces) == 1 && namespaces[0].NsId == committerpb.ConfigNamespaceID
2✔
116
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc