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

lightningnetwork / lnd / 12312390362

13 Dec 2024 08:44AM UTC coverage: 57.458% (+8.5%) from 48.92%
12312390362

Pull #9343

github

ellemouton
fn: rework the ContextGuard and add tests

In this commit, the ContextGuard struct is re-worked such that the
context that its new main WithCtx method provides is cancelled in sync
with a parent context being cancelled or with it's quit channel being
cancelled. Tests are added to assert the behaviour. In order for the
close of the quit channel to be consistent with the cancelling of the
derived context, the quit channel _must_ be contained internal to the
ContextGuard so that callers are only able to close the channel via the
exposed Quit method which will then take care to first cancel any
derived context that depend on the quit channel before returning.
Pull Request #9343: fn: expand the ContextGuard and add tests

101853 of 177264 relevant lines covered (57.46%)

24972.93 hits per line

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

21.74
/build/version.go
1
// Copyright (c) 2013-2017 The btcsuite developers
2
// Copyright (c) 2015-2016 The Decred developers
3
// Heavily inspired by https://github.com/btcsuite/btcd/blob/master/version.go
4
// Copyright (C) 2015-2022 The Lightning Network Developers
5

6
package build
7

8
import (
9
        "context"
10
        "encoding/hex"
11
        "fmt"
12
        "runtime/debug"
13
        "strings"
14

15
        "github.com/btcsuite/btclog/v2"
16
)
17

18
var (
19
        // Commit stores the current commit of this build, which includes the
20
        // most recent tag, the number of commits since that tag (if non-zero),
21
        // the commit hash, and a dirty marker. This should be set using the
22
        // -ldflags during compilation.
23
        Commit string
24

25
        // CommitHash stores the current commit hash of this build.
26
        CommitHash string
27

28
        // RawTags contains the raw set of build tags, separated by commas.
29
        RawTags string
30

31
        // GoVersion stores the go version that the executable was compiled
32
        // with.
33
        GoVersion string
34
)
35

36
// semanticAlphabet is the set of characters that are permitted for use in an
37
// AppPreRelease.
38
const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-."
39

40
// These constants define the application version and follow the semantic
41
// versioning 2.0.0 spec (http://semver.org/).
42
const (
43
        // AppMajor defines the major version of this binary.
44
        AppMajor uint = 0
45

46
        // AppMinor defines the minor version of this binary.
47
        AppMinor uint = 18
48

49
        // AppPatch defines the application patch for this binary.
50
        AppPatch uint = 99
51

52
        // AppPreRelease MUST only contain characters from semanticAlphabet per
53
        // the semantic versioning spec.
54
        AppPreRelease = "beta"
55
)
56

57
func init() {
73✔
58
        // Assert that AppPreRelease is valid according to the semantic
73✔
59
        // versioning guidelines for pre-release version and build metadata
73✔
60
        // strings. In particular it MUST only contain characters in
73✔
61
        // semanticAlphabet.
73✔
62
        for _, r := range AppPreRelease {
365✔
63
                if !strings.ContainsRune(semanticAlphabet, r) {
292✔
64
                        panic(fmt.Errorf("rune: %v is not in the semantic "+
×
65
                                "alphabet", r))
×
66
                }
67
        }
68

69
        // Get build information from the runtime.
70
        if info, ok := debug.ReadBuildInfo(); ok {
146✔
71
                GoVersion = info.GoVersion
73✔
72
                for _, setting := range info.Settings {
73✔
73
                        switch setting.Key {
×
74
                        case "vcs.revision":
×
75
                                CommitHash = setting.Value
×
76

77
                        case "-tags":
×
78
                                RawTags = setting.Value
×
79
                        }
80
                }
81
        }
82
}
83

84
// Version returns the application version as a properly formed string per the
85
// semantic versioning 2.0.0 spec (http://semver.org/).
86
func Version() string {
×
87
        // Start with the major, minor, and patch versions.
×
88
        version := fmt.Sprintf("%d.%d.%d", AppMajor, AppMinor, AppPatch)
×
89

×
90
        // Append pre-release version if there is one. The hyphen called for by
×
91
        // the semantic versioning spec is automatically appended and should not
×
92
        // be contained in the pre-release string.
×
93
        if AppPreRelease != "" {
×
94
                version = fmt.Sprintf("%s-%s", version, AppPreRelease)
×
95
        }
×
96

97
        return version
×
98
}
99

100
// Tags returns the list of build tags that were compiled into the executable.
101
func Tags() []string {
×
102
        if len(RawTags) == 0 {
×
103
                return nil
×
104
        }
×
105

106
        return strings.Split(RawTags, ",")
×
107
}
108

109
// WithBuildInfo derives a child context with the build information attached as
110
// attributes. At the moment, this only includes the current build's commit
111
// hash.
112
func WithBuildInfo(ctx context.Context, cfg *LogConfig) (context.Context,
113
        error) {
×
114

×
115
        if cfg.NoCommitHash {
×
116
                return ctx, nil
×
117
        }
×
118

119
        // Convert the commit hash to a byte slice.
120
        commitHash, err := hex.DecodeString(CommitHash)
×
121
        if err != nil {
×
122
                return nil, fmt.Errorf("unable to decode commit hash: %w", err)
×
123
        }
×
124

125
        // Include the first 3 bytes of the commit hash in the context as an
126
        // slog attribute.
127
        if len(commitHash) > 3 {
×
128
                commitHash = commitHash[:3]
×
129
        }
×
130

131
        return btclog.WithCtx(ctx, btclog.Hex("rev", commitHash)), nil
×
132
}
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

© 2025 Coveralls, Inc