Skip to content

V6 [WIP] #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c3b29d9
revamp
mredolatti Mar 26, 2024
c250cf0
switch to v6
mredolatti Mar 26, 2024
3dcce66
modernize mocks & test harness
mredolatti Mar 28, 2024
714ee30
update ci
mredolatti Mar 28, 2024
7499252
remove comparable constraint from cache value
mredolatti Apr 5, 2024
8e2163e
added SetNX into RedisClient and RedisPipeline
mmelograno May 24, 2024
abb6504
Added Result handler for Eval method
mmelograno May 27, 2024
d29ee10
added mock for Val
mmelograno May 27, 2024
544d3ac
add deref
mredolatti Jul 25, 2024
f4ba753
fix test
mredolatti Jul 25, 2024
c852721
Merge pull request #93 from splitio/deref
mmelograno Jul 30, 2024
d2571ad
allow manually evicting a key
mredolatti Aug 22, 2024
a3d143b
Merge pull request #94 from splitio/cache/purgebykey
mredolatti Aug 22, 2024
f9207aa
Merge branch 'v6' of github.com:splitio/go-toolkit into v6
mmelograno Dec 6, 2024
109c5c2
added log with ctx
mmelograno Dec 10, 2024
3dfc9b4
improvements into code
mmelograno Dec 11, 2024
279bdc9
made delegate with ctx
mmelograno Dec 13, 2024
00cf0fa
added base context
mmelograno Dec 13, 2024
ae7e875
removed baseline
mmelograno Dec 16, 2024
5436b9e
Updated License Year
github-actions[bot] Jan 1, 2025
bab3022
Merge pull request #92 from splitio/task/SetNX
mmelograno Jan 2, 2025
006c0f4
using strings builder instead concat
mmelograno Jan 3, 2025
5c67af2
Merge pull request #96 from splitio/update-license
mmelograno Jan 3, 2025
2243aa6
Merge pull request #95 from splitio/task/loggerWithCtx
mmelograno Jan 3, 2025
c923d31
Merge branch 'v6' of github.com:splitio/go-toolkit into v6
mmelograno Jan 24, 2025
5d10a4d
added isSubset
mmelograno Jan 24, 2025
3591501
compatibility with isSubset on previous version
mmelograno Jan 24, 2025
8dd9162
added isequal
mmelograno Jan 24, 2025
7bad205
augment logger
mmelograno Feb 18, 2025
de96baf
Merge pull request #97 from splitio/task/setMethods
mmelograno Feb 20, 2025
7d961a8
Merge branch 'v6' of github.com:splitio/go-toolkit into task/augmentL…
mmelograno Feb 20, 2025
2d26bfd
Merge pull request #98 from splitio/task/augmentLogging
mmelograno Feb 20, 2025
a696817
Merge branch 'v6' of github.com:splitio/go-toolkit into v6
mmelograno Feb 28, 2025
def4d91
add clone method for log
mmelograno May 6, 2025
fb49ce7
skipFrame
mmelograno May 6, 2025
8e72995
Merge pull request #99 from splitio/task/clone
mmelograno May 6, 2025
d9feb24
Merge branch 'v6' of github.com:splitio/go-toolkit into v6
mmelograno May 6, 2025
14308c6
accept only 1 struct
mmelograno May 9, 2025
4ba06ae
Merge pull request #100 from splitio/task/cloneL
mmelograno May 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ jobs:
- name: Set up Go version
uses: actions/setup-go@v3
with:
go-version: '1.18.0'
go-version: '1.21.0'

- name: Go mod
run: go mod tidy

- name: Execute tests
run: go test -coverprofile=coverage.out -count=1 -race ./...
run: make test

- name: SonarQube Scan (Push)
if: github.event_name == 'push'
Expand Down
10 changes: 10 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
v6.0.0 (TBD):
- BREAKING CHANGE:
- Updated common helpers to be generic
- Updated datastructures to be generic
- Cleanup package structure and remove deprecated ones
- Updated logger with formatting functionality
- Modernized test harness & mocks
- Added SetNX operation to RedisClient and RedisPipeline.
- Added Val() result to Eval operation for RedisClient.

5.4.0 (Jan 10, 2024)
- Added `Scan` operation to Redis

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright © 2024 Split Software, Inc.
Copyright © 2025 Split Software, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
GO ?= go
COVERAGE_OUT = coverage.out

.PHONY: test test-norace

test:
$(GO) test ./... -count=1 -race -coverprofile=$(COVERAGE_OUT)

test-norace:
$(GO) test ./... -count=1

4 changes: 2 additions & 2 deletions asynctask/asynctasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"time"

"github.com/splitio/go-toolkit/v5/logging"
"github.com/splitio/go-toolkit/v5/struct/traits/lifecycle"
"github.com/splitio/go-toolkit/v6/logging"
"github.com/splitio/go-toolkit/v6/struct/traits/lifecycle"
)

// AsyncTask is a struct that wraps tasks that should run periodically and can be remotely stopped & started,
Expand Down
50 changes: 15 additions & 35 deletions asynctask/asynctasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"testing"
"time"

"github.com/splitio/go-toolkit/v5/logging"
"github.com/splitio/go-toolkit/v6/logging"
"github.com/stretchr/testify/assert"
)

func TestAsyncTaskNormalOperation(t *testing.T) {
Expand All @@ -29,27 +30,14 @@ func TestAsyncTaskNormalOperation(t *testing.T) {

task1.Start()
time.Sleep(1 * time.Second)
if !task1.IsRunning() {
t.Error("Task should be running")
}
time.Sleep(1 * time.Second)
assert.True(t, task1.IsRunning())

time.Sleep(1 * time.Second)
task1.Stop(true)
if task1.IsRunning() {
t.Error("Task should be stopped")
}

if !onInit.Load().(bool) {
t.Error("Initialization hook not executed")
}

if !onExecution.Load().(bool) {
t.Error("Main task function not executed")
}

if !onStop.Load().(bool) {
t.Error("After execution function not executed")
}
assert.False(t, task1.IsRunning())
assert.True(t, onInit.Load().(bool))
assert.True(t, onExecution.Load().(bool))
assert.True(t, onStop.Load().(bool))
}

func TestAsyncTaskPanics(t *testing.T) {
Expand Down Expand Up @@ -94,15 +82,10 @@ func TestAsyncTaskPanics(t *testing.T) {
task3.Start()
time.Sleep(time.Second * 2)
task3.Stop(true)
if task1.IsRunning() {
t.Error("Task1 is running and should be stopped")
}
if task2.IsRunning() {
t.Error("Task2 is running and should be stopped")
}
if task3.IsRunning() {
t.Error("Task3 is running and should be stopped")
}

assert.False(t, task1.IsRunning())
assert.False(t, task2.IsRunning())
assert.False(t, task3.IsRunning())
}

func TestAsyncTaskErrors(t *testing.T) {
Expand Down Expand Up @@ -138,9 +121,8 @@ func TestAsyncTaskErrors(t *testing.T) {

task2.Start()
time.Sleep(2 * time.Second)
if res.Load().(int) != 0 {
t.Error("Task should have never executed if there was an error when calling onInit()")
}

assert.Equal(t, int(0), res.Load().(int))
}

func TestAsyncTaskWakeUp(t *testing.T) {
Expand All @@ -163,7 +145,5 @@ func TestAsyncTaskWakeUp(t *testing.T) {
_ = task1.WakeUp()
_ = task1.Stop(true)

if atomic.LoadInt32(&res) != 3 {
t.Errorf("Task shuld have executed 4 times. It ran %d times", res)
}
assert.Equal(t, int32(3), atomic.LoadInt32(&res))
}
30 changes: 9 additions & 21 deletions backoff/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,20 @@ package backoff
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)

func TestBackoff(t *testing.T) {
base := int64(10)
maxAllowed := 60 * time.Second
backoff := New(base, maxAllowed)
if backoff.base != base {
t.Error("It should be equals to 10")
}
if backoff.maxAllowed != maxAllowed {
t.Error("It should be equals to 60")
}
if backoff.Next() != 1*time.Second {
t.Error("It should be 1 second")
}
if backoff.Next() != 10*time.Second {
t.Error("It should be 10 seconds")
}
if backoff.Next() != 60*time.Second {
t.Error("It should be 60 seconds")
}
assert.Equal(t, base, backoff.base)
assert.Equal(t, maxAllowed, backoff.maxAllowed)
assert.Equal(t, 1*time.Second, backoff.Next())
assert.Equal(t, 10*time.Second, backoff.Next())
assert.Equal(t, 60*time.Second, backoff.Next())

backoff.Reset()
if backoff.current != 0 {
t.Error("It should be zero")
}
if backoff.Next() != 1*time.Second {
t.Error("It should be 1 second")
}
assert.Equal(t, int64(0), backoff.current)
assert.Equal(t, 1*time.Second, backoff.Next())
}
17 changes: 12 additions & 5 deletions backoff/mocks/mocks.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package mocks

import "time"
import (
"github.com/splitio/go-toolkit/v6/backoff"
"github.com/stretchr/testify/mock"
"time"
)

type BackoffMock struct {
NextCall func() time.Duration
ResetCall func()
mock.Mock
}

// Next implements backoff.Interface.
func (b *BackoffMock) Next() time.Duration {
return b.NextCall()
return b.Called().Get(0).(time.Duration)
}

// Reset implements backoff.Interface.
func (b *BackoffMock) Reset() {
b.ResetCall()
b.Called()
}

var _ backoff.Interface = (*BackoffMock)(nil)
111 changes: 111 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package common

import "cmp"

// New helpers to be used when with newer go versions.
// the rest of the common package should be removed in v3, and consumers of the lib
// should only rely on these functions

// Ref creates a copy of `x` in heap and returns a pointer to it
func Ref[T any](x T) *T {
return &x
}

// RefOrNil returns a pointer to the value supplied if it's not the default value, nil otherwise
func RefOrNil[T comparable](x T) *T {
var t T
if x == t {
return nil
}
return &x
}

// PointerOf performs a type-assertion to T and returns a pointer if successful, nil otherwise.
func PointerOf[T any](x interface{}) *T {
if x == nil {
return nil
}

ta, ok := x.(T)
if !ok {
return nil
}

return &ta
}

// DerefOr returns value pointed by `tp` or the fallback supplied
func DerefOr[T any](tp *T, fallback T) T {
if tp == nil {
return fallback
}
return *tp
}

// PartitionSliceByLength partitions a slice into multiple slices of up to `maxItems` size
func PartitionSliceByLength[T comparable](items []T, maxItems int) [][]T {
var splitted [][]T
for i := 0; i < len(items); i += maxItems {
end := i + maxItems
if end > len(items) {
end = len(items)
}
splitted = append(splitted, items[i:end])
}
return splitted
}

// DedupeInNewSlice creates a new slice from `items` without duplicate elements
func UnorderedDedupedCopy[T comparable](items []T) []T {
present := make(map[T]struct{}, len(items))
for idx := range items {
present[items[idx]] = struct{}{}
}

ret := make([]T, 0, len(present))
for key := range present {
ret = append(ret, key)
}

return ret
}

// ValueOr returns the supplied value if it has something other than the default value
// for type T. Returns `fallback` otherwise
func ValueOr[T comparable](in T, fallback T) T {
var t T
if in == t {
return fallback
}
return in
}

// Max returns the greatest item of all supplied
func Max[T cmp.Ordered](i1 T, rest ...T) T {
max := i1
for idx := range rest {
if rest[idx] > max {
max = rest[idx]
}
}
return max
}

// Min returns the minimum item of all supplied
func Min[T cmp.Ordered](i1 T, rest ...T) T {
min := i1
for idx := range rest {
if rest[idx] < min {
min = rest[idx]
}
}
return min
}

func AsInterfaceSlice[T any](in []T) []interface{} {
out := make([]interface{}, len(in))
for idx := range in {
out[idx] = in[idx]
}
return out
}
Loading