-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor opencdc record, include serializer, rename ctxutil to cconte…
…xt, include csync functions from connector-sdk
- Loading branch information
1 parent
1c5139d
commit 469a7c0
Showing
14 changed files
with
479 additions
and
207 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,47 @@ | ||
// 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" | ||
"time" | ||
|
||
"github.com/conduitio/conduit-commons/cchan" | ||
) | ||
|
||
// Run executes fn in a goroutine and waits for it to return. If the context | ||
// gets canceled before that happens the method returns the context error. | ||
// | ||
// This is useful for executing long-running functions like sync.WaitGroup.Wait | ||
// that don't take a context and can potentially block the execution forever. | ||
func Run(ctx context.Context, fn func()) error { | ||
done := make(chan struct{}) | ||
go func() { | ||
defer close(done) | ||
fn() | ||
}() | ||
|
||
_, _, err := cchan.ChanOut[struct{}](done).Recv(ctx) | ||
return err //nolint:wrapcheck // errors from cchan are context errors and we don't wrap them | ||
} | ||
|
||
// RunTimeout executes fn in a goroutine and waits for it to return. If the | ||
// context gets canceled before that happens or the timeout is reached the | ||
// method returns the context error. | ||
func RunTimeout(ctx context.Context, fn func(), timeout time.Duration) error { | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
return Run(ctx, fn) | ||
} |
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 := RunTimeout(ctx, func() { <-time.After(time.Second) }, time.Millisecond*100) | ||
since := time.Since(start) | ||
|
||
is.Equal(err, context.DeadlineExceeded) | ||
is.True(since >= time.Millisecond*100) | ||
} |
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
Oops, something went wrong.