This commit is contained in:
skiffer-git 2024-04-16 10:36:51 +08:00
parent 5b7379f531
commit b96ea0fa4a
2 changed files with 41 additions and 209 deletions

View File

@ -13,141 +13,3 @@
// limitations under the License. // limitations under the License.
package webhook package webhook
import (
"context"
"testing"
"github.com/openimsdk/open-im-server/v3/pkg/callbackstruct"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
)
//func TestGet(t *testing.T) {
// type args struct {
// url string
// }
// tests := []struct {
// name string
// args args
// wantResponse []byte
// wantErr bool
// }{
// // TODO: Add test cases.
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// gotResponse, err := Get(tt.args.url)
// if (err != nil) != tt.wantErr {
// t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
// return
// }
// if !reflect.DeepEqual(gotResponse, tt.wantResponse) {
// t.Errorf("Get() = %v, want %v", gotResponse, tt.wantResponse)
// }
// })
// }
//}
//func TestPost(t *testing.T) {
// type args struct {
// ctx context.Context
// url string
// header map[string]string
// data any
// timeout int
// }
// tests := []struct {
// name string
// args args
// wantContent []byte
// wantErr bool
// }{
// // TODO: Add test cases.
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// gotContent, err := Post(tt.args.ctx, tt.args.url, tt.args.header, tt.args.data, tt.args.timeout)
// if (err != nil) != tt.wantErr {
// t.Errorf("Post() error = %v, wantErr %v", err, tt.wantErr)
// return
// }
// if !reflect.DeepEqual(gotContent, tt.wantContent) {
// t.Errorf("Post() = %v, want %v", gotContent, tt.wantContent)
// }
// })
// }
//}
//func TestPostReturn(t *testing.T) {
// type args struct {
// ctx context.Context
// url string
// header map[string]string
// input any
// output any
// timeOutSecond int
// }
// tests := []struct {
// name string
// args args
// wantErr bool
// }{
// // TODO: Add test cases.
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// if err := PostReturn(tt.args.ctx, tt.args.url, tt.args.header, tt.args.input, tt.args.output, tt.args.timeOutSecond); (err != nil) != tt.wantErr {
// t.Errorf("PostReturn() error = %v, wantErr %v", err, tt.wantErr)
// }
// })
// }
//}
func Test_callBackPostReturn(t *testing.T) {
type args struct {
ctx context.Context
url string
command string
input any
output callbackstruct.CallbackResp
callbackConfig config.CallBackConfig
}
tests := []struct {
name string
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := callBackPostReturn(tt.args.ctx, tt.args.url, tt.args.command, tt.args.input, tt.args.output, tt.args.callbackConfig); (err != nil) != tt.wantErr {
t.Errorf("callBackPostReturn() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestCallBackPostReturn(t *testing.T) {
type args struct {
ctx context.Context
url string
req callbackstruct.CallbackReq
resp callbackstruct.CallbackResp
callbackConfig config.CallBackConfig
}
tests := []struct {
name string
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CallBackPostReturn(tt.args.ctx, tt.args.url, tt.args.req, tt.args.resp, tt.args.callbackConfig); (err != nil) != tt.wantErr {
t.Errorf("CallBackPostReturn() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -1,91 +1,61 @@
package memAsyncQueue package memAsyncQueue
import ( import (
"sync"
"testing" "testing"
"time" "time"
) )
// TestPushSuccess tests the successful pushing of data into the queue. func TestNewMemoryQueue(t *testing.T) {
func TestPushSuccess(t *testing.T) { workerCount := 3
queue := &MemoryQueue{} bufferSize := 10
queue.Initialize(func(data any) {}, 1, 5) // Small buffer size for test queue := NewMemoryQueue(workerCount, bufferSize)
// Try to push data that should succeed if cap(queue.taskChan) != bufferSize {
err := queue.Push("test data") t.Errorf("Expected buffer size %d, got %d", bufferSize, cap(queue.taskChan))
if err != nil { }
t.Errorf("Push should succeed, but got error: %v", err)
if queue.isStopped {
t.Errorf("New queue is prematurely stopped")
}
if len(queue.taskChan) != 0 {
t.Errorf("New queue should be empty, found %d items", len(queue.taskChan))
} }
} }
// TestPushFailWhenFull tests that pushing to a full queue results in an error. func TestPushAndStop(t *testing.T) {
func TestPushFailWhenFull(t *testing.T) { queue := NewMemoryQueue(1, 5)
queue := &MemoryQueue{}
queue.Initialize(func(data any) {
time.Sleep(100 * time.Millisecond) // Simulate work to delay processing
}, 1, 1) // Very small buffer to fill quickly
queue.Push("data 1") // Fill the buffer var wg sync.WaitGroup
err := queue.Push("data 2") // This should fail wg.Add(1)
queue.Push(func() {
time.Sleep(50 * time.Millisecond) // Simulate task delay
wg.Done()
})
if err == nil { queue.Stop()
t.Error("Expected an error when pushing to full queue, but got none") wg.Wait()
if err := queue.Push(func() {}); err == nil {
t.Error("Expected error when pushing to stopped queue, got none")
} }
} }
// TestPushFailWhenStopped tests that pushing to a stopped queue results in an error. func TestPushTimeout(t *testing.T) {
func TestPushFailWhenStopped(t *testing.T) { queue := NewMemoryQueue(1, 1) // Small buffer and worker to force full queue
queue := &MemoryQueue{}
queue.Initialize(func(data any) {}, 1, 1)
queue.Stop() // Stop the queue before pushing done := make(chan bool)
err := queue.Push("test data") go func() {
queue.Push(func() {
time.Sleep(200 * time.Millisecond) // Long enough to cause the second push to timeout
})
done <- true
}()
if err == nil { <-done // Ensure first task is pushed
t.Error("Expected an error when pushing to stopped queue, but got none")
} if err := queue.Push(func() {}); err != nil {
} t.Error("Expected timeout error, got nil")
// TestQueueOperationSequence tests a sequence of operations to ensure the queue handles them correctly.
func TestQueueOperationSequence(t *testing.T) {
queue := &MemoryQueue{}
queue.Initialize(func(data any) {}, 1, 2)
// Sequence of pushes and a stop
err := queue.Push("data 1")
if err != nil {
t.Errorf("Failed to push data 1: %v", err)
}
err = queue.Push("data 2")
if err != nil {
t.Errorf("Failed to push data 2: %v", err)
}
queue.Stop() // Stop the queue
err = queue.Push("data 3") // This push should fail
if err == nil {
t.Error("Expected an error when pushing after stop, but got none")
}
}
// TestBlockingOnFull tests that the queue does not block indefinitely when full.
func TestBlockingOnFull(t *testing.T) {
queue := &MemoryQueue{}
queue.Initialize(func(data any) {
time.Sleep(1 * time.Second) // Simulate a long processing time
}, 1, 1)
queue.Push("data 1") // Fill the queue
start := time.Now()
err := queue.Push("data 2") // This should time out
duration := time.Since(start)
if err == nil {
t.Error("Expected an error due to full queue, but got none")
}
if duration >= time.Second {
t.Errorf("Push blocked for too long, duration: %v", duration)
} }
} }