-
Notifications
You must be signed in to change notification settings - Fork 18
tests: add tests for chan utils #140
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9ef9bc0
add unit-test
wyyolo 9dfdb61
add unit-test
wyyolo 1f38134
Update common/chan_utils_test.go
RobertIndie cfb9e56
add unit-test, correct issues
wyyolo f6bfa08
Merge remote-tracking branch 'origin/unit-test' into unit-test
wyyolo 7fab48f
add unit-test, correct issues
wyyolo fc86e3e
add unit-test, Modify SendToChannel and ReceiveFromChannel logic
wyyolo fe27a9f
chan_utils unit-test
wyyolo b606d3f
chan_utils fix:unit-test
wyyolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,200 @@ | ||
| /* | ||
| * Copyright 2024 Function Stream Org. | ||
| * | ||
| * 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 common | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestSendToChannel(t *testing.T) { | ||
|
|
||
| //send data | ||
| //The first t.Run is a buffered chan, and the second t.Run is an unbuffered chan | ||
| t.Run("send success,Buffered chan", func(t *testing.T) { | ||
|
wyyolo marked this conversation as resolved.
Outdated
|
||
| c := make(chan string, 1) | ||
| ctx := context.Background() | ||
| if !SendToChannel(ctx, c, "data") { | ||
| t.Fatal("SendToChannel should return true when sending succeeds") | ||
| } | ||
| value := <-c | ||
| // Verify if the received data is correct | ||
| if value != "data" { | ||
| t.Errorf("expected to receive \"data\" from channel, but received %s", value) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("send success,Unbuffered chan", func(t *testing.T) { | ||
| c := make(chan string) | ||
| ctx := context.Background() | ||
| var wg sync.WaitGroup | ||
| wg.Add(2) | ||
| go func() { | ||
| defer wg.Done() | ||
| SendToChannel(ctx, c, "data") | ||
| }() | ||
| go func() { | ||
| defer wg.Done() | ||
| value := <-c | ||
| // Verify if the received data is correct | ||
| if value != "data" { | ||
| t.Errorf("expected to receive \"data\" from channel, but received %s", value) | ||
| } | ||
| }() | ||
|
wyyolo marked this conversation as resolved.
Outdated
|
||
| wg.Wait() | ||
| }) | ||
|
|
||
| //context timeout | ||
| //The first t.Run is a test with a timeout setting, but it was successfully sent without timeout | ||
| //The second t.Run passes through time.Sleep setting timeout, simulating context timeout | ||
| t.Run("context not timeout", func(t *testing.T) { | ||
| c := make(chan string, 1) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
| defer cancel() | ||
| if SendToChannel(ctx, c, "hello") { | ||
| t.Log("Data sent successfully") | ||
| } else { | ||
| fmt.Println(SendToChannel(ctx, c, "hello")) | ||
|
wyyolo marked this conversation as resolved.
Outdated
|
||
| t.Fatal("Failed to send data due to context timeout") | ||
| } | ||
| select { | ||
| case <-c: | ||
| t.Log("Successfully received data") | ||
| case <-ctx.Done(): | ||
| t.Fatal("Channel closed after context timeout") | ||
|
wyyolo marked this conversation as resolved.
Outdated
|
||
| } | ||
| }) | ||
|
|
||
| t.Run("context timeout", func(t *testing.T) { | ||
| c := make(chan string, 1) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
| defer cancel() | ||
| if SendToChannel(ctx, c, "data") { | ||
| t.Log("Data sent successfully") | ||
| } else { | ||
| t.Fatal("Failed to send data due to context timeout") | ||
| } | ||
|
|
||
| go func() { | ||
| time.Sleep(time.Second) // Set timeout | ||
| select { | ||
| case <-c: | ||
| t.Error("Timed out but able to retrieve data") | ||
| return | ||
| case <-ctx.Done(): | ||
| t.Log("No data received due to context timeout") | ||
| } | ||
| }() | ||
| }) | ||
|
|
||
| //incorrect type | ||
| //It is not necessary to distinguish between buffered and unbuffered, as it is necessary to test the incorrect type | ||
| t.Run("incorrect type", func(t *testing.T) { | ||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| t.Log("test-ok") | ||
| } else { | ||
| t.Log("test-fail") | ||
| } | ||
| }() | ||
| c := make(chan int) | ||
| ctx := context.Background() | ||
| SendToChannel(ctx, c, "incorrect type") | ||
| }) | ||
| } | ||
|
|
||
| func TestZeroValue(t *testing.T) { | ||
| tests := []struct { | ||
| Type string | ||
| want interface{} | ||
| }{ | ||
| {Type: "int", want: 0}, | ||
| {Type: "string", want: ""}, | ||
| {Type: "bool", want: false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.Type, func(t *testing.T) { | ||
| zero := zeroValue[interface{}]() | ||
| switch v := zero.(type) { | ||
| case int: | ||
| if v != 0 { | ||
| t.Errorf("zeroValue = %v, want 0", v) | ||
| } | ||
| case string: | ||
| if v != "" { | ||
| t.Errorf("zeroValue = %v, want \"\"", v) | ||
| } | ||
| case bool: | ||
| if v != false { | ||
| t.Errorf("zeroValue = %v, want false", v) | ||
| } | ||
| default: | ||
| t.Log("ok") | ||
| } | ||
| }) | ||
|
RobertIndie marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
RobertIndie marked this conversation as resolved.
|
||
|
|
||
| func TestReceiveFromChannel(t *testing.T) { | ||
|
|
||
| //Since SendToChannel has already been tested, only buffered chan will be considered here | ||
|
wyyolo marked this conversation as resolved.
Outdated
|
||
| //Successfully received chan data | ||
| t.Run("Success", func(t *testing.T) { | ||
| ctx := context.Background() | ||
| ch := make(chan string, 1) | ||
| SendToChannel(ctx, ch, "test-data") | ||
| value, _ := ReceiveFromChannel(ctx, ch) | ||
|
wyyolo marked this conversation as resolved.
Outdated
|
||
| if value != "test-data" { | ||
| t.Errorf("Receive failed,Expected value to be \"test-data\", but %s", value) | ||
|
wyyolo marked this conversation as resolved.
Outdated
|
||
| } | ||
| }) | ||
|
|
||
| //context timeout | ||
| t.Run("Timeout", func(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
| defer cancel() | ||
| time.Sleep(10 * time.Millisecond) | ||
| ch := make(chan string, 1) | ||
| //No need to send data to SendToChannel as the context has been set to expire | ||
| value, ok := ReceiveFromChannel(ctx, ch) | ||
| if ok { | ||
| t.Fatal("Due to timeout setting, it is expected that no value will be received from the channel") | ||
| } | ||
| if value != "" { | ||
| t.Errorf("Expected zero value for string, but %s", value) | ||
| } | ||
| }) | ||
|
|
||
| //context canceled | ||
| t.Run("Canceled", func(t *testing.T) { | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| //Cancel context | ||
| cancel() | ||
| ch := make(chan string, 1) | ||
| value, ok := ReceiveFromChannel(ctx, ch) | ||
| if ok { | ||
| t.Fatal("Expected no value to be received from channel due to context cancellation") | ||
| } | ||
| if value != "" { | ||
| t.Errorf("Expected zero value for string, but %s", value) | ||
| } | ||
| }) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.