-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* csync run tests * track connector state * make connector state watcher safer to use * remove commented code * improve states * update golangci-lint * update golangci-lint v1.55.2 * add test for source stop * check error in test * fix comment Co-authored-by: Maha Hajja <[email protected]> * organize imports, simplify teardown * comment about timeouts on Stop --------- Co-authored-by: Maha Hajja <[email protected]>
- Loading branch information
1 parent
e83c8d6
commit 4d9c681
Showing
11 changed files
with
617 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright © 2023 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:generate stringer -type ConnectorState -trimprefix State | ||
|
||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"slices" | ||
|
||
"github.com/conduitio/conduit-connector-sdk/internal/csync" | ||
) | ||
|
||
type ConnectorState int | ||
|
||
const ( | ||
StateInitial ConnectorState = iota | ||
StateConfiguring | ||
StateConfigured | ||
StateStarting | ||
StateStarted | ||
StateInitiatingRun | ||
StateRunning | ||
StateInitiatingStop | ||
StateStopping | ||
StateStopped | ||
StateTearingDown | ||
StateTornDown | ||
|
||
StateErrored ConnectorState = 500 | ||
) | ||
|
||
type ConnectorStateWatcher csync.ValueWatcher[ConnectorState] | ||
|
||
type DoWithLockOptions struct { | ||
ExpectedStates []ConnectorState | ||
StateBefore ConnectorState | ||
StateAfter ConnectorState | ||
WaitForExpectedState bool | ||
} | ||
|
||
func (w *ConnectorStateWatcher) DoWithLock( | ||
ctx context.Context, | ||
opts DoWithLockOptions, | ||
f func(currentState ConnectorState) error, | ||
) error { | ||
vw := (*csync.ValueWatcher[ConnectorState])(w) | ||
lockedWatcher := vw.Lock() | ||
locked := true // keep track if the lock is still locked | ||
defer func() { | ||
if locked { | ||
lockedWatcher.Unlock() | ||
} | ||
}() | ||
|
||
currentState := lockedWatcher.Get() | ||
|
||
if len(opts.ExpectedStates) > 0 { | ||
for !slices.Contains(opts.ExpectedStates, currentState) { | ||
if !opts.WaitForExpectedState { | ||
return fmt.Errorf("expected connector state %q, actual connector state is %q", opts.ExpectedStates, currentState) | ||
} | ||
lockedWatcher.Unlock() | ||
lockedWatcher = nil // discard locked watcher after unlock | ||
locked = false // prevent another unlock in defer | ||
|
||
_, err := vw.Watch(ctx, csync.WatchValues(opts.ExpectedStates...)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// lock watcher again and check current state in case it changed between | ||
// watch and the second lock | ||
lockedWatcher = vw.Lock() | ||
locked = true | ||
currentState = lockedWatcher.Get() | ||
} | ||
} | ||
|
||
w.swap(lockedWatcher, opts.StateBefore) | ||
|
||
err := f(currentState) | ||
if err != nil { | ||
lockedWatcher.Set(StateErrored) | ||
return err | ||
} | ||
|
||
w.swap(lockedWatcher, opts.StateAfter) | ||
return nil | ||
} | ||
|
||
func (w *ConnectorStateWatcher) Set(newState ConnectorState) bool { | ||
lockedWatcher := (*csync.ValueWatcher[ConnectorState])(w).Lock() | ||
defer lockedWatcher.Unlock() | ||
return w.swap(lockedWatcher, newState) | ||
} | ||
|
||
func (w *ConnectorStateWatcher) swap(lvw *csync.LockedValueWatcher[ConnectorState], newState ConnectorState) bool { | ||
if lvw.Get() >= newState { | ||
// states can only increase | ||
return false | ||
} | ||
lvw.Set(newState) | ||
return true | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright © 2023 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package csync | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/matryer/is" | ||
) | ||
|
||
func TestRun_Success(t *testing.T) { | ||
is := is.New(t) | ||
ctx := context.Background() | ||
|
||
var executed bool | ||
err := Run(ctx, func() { executed = true }) | ||
is.NoErr(err) | ||
is.True(executed) | ||
} | ||
|
||
func TestRun_Canceled(t *testing.T) { | ||
is := is.New(t) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
|
||
// run function that blocks for 1 second | ||
err := Run(ctx, func() { <-time.After(time.Second) }) | ||
is.Equal(err, context.Canceled) | ||
} | ||
|
||
func TestRun_DeadlineReached(t *testing.T) { | ||
is := is.New(t) | ||
ctx := context.Background() | ||
|
||
start := time.Now() | ||
err := Run(ctx, func() { <-time.After(time.Second) }, WithTimeout(time.Millisecond*100)) | ||
since := time.Since(start) | ||
|
||
is.Equal(err, context.DeadlineExceeded) | ||
is.True(since >= time.Millisecond*100) | ||
} |
Oops, something went wrong.