Skip to content

Commit e4c4793

Browse files
author
amuraru
committed
Add unit tests and golangci lint CI check
1 parent be6d416 commit e4c4793

File tree

14 files changed

+172
-195
lines changed

14 files changed

+172
-195
lines changed

.github/workflows/ci.yaml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: true
12+
13+
env:
14+
GO_VERSION: '1.25'
15+
16+
jobs:
17+
test:
18+
name: Test
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v5
23+
24+
- name: Set up Go
25+
uses: actions/setup-go@v5
26+
with:
27+
go-version: ${{ env.GO_VERSION }}
28+
cache: true
29+
30+
- name: Download dependencies
31+
run: go mod download
32+
33+
- name: Verify dependencies
34+
run: go mod verify
35+
36+
- name: Run tests
37+
run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./...
38+
39+
- name: Upload coverage to Codecov
40+
uses: codecov/codecov-action@v4
41+
with:
42+
files: ./coverage.out
43+
flags: unittests
44+
fail_ci_if_error: false
45+
46+
lint:
47+
name: Lint
48+
runs-on: ubuntu-latest
49+
steps:
50+
- name: Checkout code
51+
uses: actions/checkout@v5
52+
53+
- name: Set up Go
54+
uses: actions/setup-go@v5
55+
with:
56+
go-version: ${{ env.GO_VERSION }}
57+
cache: true
58+
59+
- name: Run golangci-lint
60+
uses: golangci/golangci-lint-action@v8
61+
with:
62+
version: latest
63+
args: --timeout=5m
64+
65+
build:
66+
name: Build
67+
runs-on: ubuntu-latest
68+
steps:
69+
- name: Checkout code
70+
uses: actions/checkout@v5
71+
72+
- name: Set up Go
73+
uses: actions/setup-go@v5
74+
with:
75+
go-version: ${{ env.GO_VERSION }}
76+
cache: true
77+
78+
- name: Build
79+
run: go build -v ./...
80+
81+
- name: Build binary
82+
run: go build -v -o kminion .
83+

config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ func newConfig(logger *zap.Logger) (Config, error) {
8787
}
8888

8989
err = k.Load(env.ProviderWithValue("", ".", func(s string, v string) (string, interface{}) {
90-
// key := strings.Replace(strings.ToLower(s), "_", ".", -1)
91-
key := strings.Replace(strings.ToLower(s), "_", ".", -1)
90+
key := strings.ReplaceAll(strings.ToLower(s), "_", ".")
9291
// Check to exist if we have a configuration option already and see if it's a slice
9392
// If there is a comma in the value, split the value into a slice by the comma.
9493
if strings.Contains(v, ",") {

e2e/message_tracker.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type messageTracker struct {
2828
func newMessageTracker(svc *Service) *messageTracker {
2929
defaultExpirationDuration := svc.config.Consumer.RoundtripSla
3030
cache := ttlcache.NewCache()
31-
cache.SetTTL(defaultExpirationDuration)
31+
_ = cache.SetTTL(defaultExpirationDuration)
3232

3333
t := &messageTracker{
3434
svc: svc,
@@ -43,12 +43,14 @@ func newMessageTracker(svc *Service) *messageTracker {
4343
}
4444

4545
func (t *messageTracker) addToTracker(msg *EndToEndMessage) {
46-
t.cache.Set(msg.MessageID, msg)
46+
_ = t.cache.Set(msg.MessageID, msg)
4747
}
4848

4949
// updateItemIfExists only updates a message if it still exists in the cache. The remaining time to live will not
5050
// be refreshed.
5151
// If it doesn't exist an ttlcache.ErrNotFound error will be returned.
52+
//
53+
//nolint:unused
5254
func (t *messageTracker) updateItemIfExists(msg *EndToEndMessage) error {
5355
_, ttl, err := t.cache.GetWithTTL(msg.MessageID)
5456
if err != nil {
@@ -59,9 +61,9 @@ func (t *messageTracker) updateItemIfExists(msg *EndToEndMessage) error {
5961
}
6062

6163
// Because the returned TTL is set to the original TTL duration (and not the remaining TTL) we have to calculate
62-
// the remaining TTL now as we want to updat the existing cache item without changing the remaining time to live.
64+
// the remaining TTL now as we want to update the existing cache item without changing the remaining time to live.
6365
expiryTimestamp := msg.creationTime().Add(ttl)
64-
remainingTTL := expiryTimestamp.Sub(time.Now())
66+
remainingTTL := time.Until(expiryTimestamp)
6567
if remainingTTL < 0 {
6668
// This entry should have been deleted already. Race condition.
6769
return ttlcache.ErrNotFound
@@ -96,7 +98,7 @@ func (t *messageTracker) onMessageArrived(arrivedMessage *EndToEndMessage) {
9698

9799
expireTime := msg.creationTime().Add(t.svc.config.Consumer.RoundtripSla)
98100
isExpired := time.Now().Before(expireTime)
99-
latency := time.Now().Sub(msg.creationTime())
101+
latency := time.Since(msg.creationTime())
100102

101103
if !isExpired {
102104
// Message arrived late, but was still in cache. We don't increment the lost counter here because eventually
@@ -114,7 +116,7 @@ func (t *messageTracker) onMessageArrived(arrivedMessage *EndToEndMessage) {
114116
t.svc.roundtripLatency.WithLabelValues(pID).Observe(latency.Seconds())
115117

116118
// Remove message from cache, so that we don't track it any longer and won't mark it as lost when the entry expires.
117-
t.cache.Remove(msg.MessageID)
119+
_ = t.cache.Remove(msg.MessageID)
118120
}
119121

120122
func (t *messageTracker) onMessageExpired(_ string, reason ttlcache.EvictionReason, value interface{}) {

e2e/topic.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ func (s *Service) getTopicMetadata(ctx context.Context) (*kmsg.MetadataResponse,
332332
return req.RequestWith(ctx, s.client)
333333
}
334334

335+
//nolint:unused
335336
func (s *Service) getTopicsConfigs(ctx context.Context, configNames []string) (*kmsg.DescribeConfigsResponse, error) {
336337
req := kmsg.NewDescribeConfigsRequest()
337338
req.IncludeDocumentation = false

e2e/topic_test.go

Lines changed: 0 additions & 165 deletions
This file was deleted.

e2e/utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ func (s *Service) logCommitErrors(r *kmsg.OffsetCommitResponse, err error) strin
6767
}
6868

6969
// brokerMetadataByBrokerID returns a map of all broker metadata keyed by their BrokerID
70+
//
71+
//nolint:unused
7072
func brokerMetadataByBrokerID(meta []kmsg.MetadataResponseBroker) map[int32]kmsg.MetadataResponseBroker {
7173
res := make(map[int32]kmsg.MetadataResponseBroker)
7274
for _, broker := range meta {
@@ -76,6 +78,8 @@ func brokerMetadataByBrokerID(meta []kmsg.MetadataResponseBroker) map[int32]kmsg
7678
}
7779

7880
// brokerMetadataByRackID returns a map of all broker metadata keyed by their Rack identifier
81+
//
82+
//nolint:unused
7983
func brokerMetadataByRackID(meta []kmsg.MetadataResponseBroker) map[string][]kmsg.MetadataResponseBroker {
8084
res := make(map[string][]kmsg.MetadataResponseBroker)
8185
for _, broker := range meta {
@@ -95,6 +99,7 @@ func pointerStrToStr(str *string) string {
9599
return *str
96100
}
97101

102+
//nolint:unused
98103
func safeUnwrap(err error) string {
99104
if err == nil {
100105
return "<nil>"

0 commit comments

Comments
 (0)