From b96ea0fa4a48d5272eeddf85cd5b03228b06afb3 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Tue, 16 Apr 2024 10:36:51 +0800 Subject: [PATCH] refactor --- pkg/common/webhook/http_client_test.go | 138 ------------------------- pkg/util/memAsyncQueue/queue_test.go | 112 ++++++++------------ 2 files changed, 41 insertions(+), 209 deletions(-) diff --git a/pkg/common/webhook/http_client_test.go b/pkg/common/webhook/http_client_test.go index 3ebf81522..3c3aeb809 100644 --- a/pkg/common/webhook/http_client_test.go +++ b/pkg/common/webhook/http_client_test.go @@ -13,141 +13,3 @@ // limitations under the License. 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) - } - }) - } -} diff --git a/pkg/util/memAsyncQueue/queue_test.go b/pkg/util/memAsyncQueue/queue_test.go index 465b5ced9..19510dd63 100644 --- a/pkg/util/memAsyncQueue/queue_test.go +++ b/pkg/util/memAsyncQueue/queue_test.go @@ -1,91 +1,61 @@ package memAsyncQueue import ( + "sync" "testing" "time" ) -// TestPushSuccess tests the successful pushing of data into the queue. -func TestPushSuccess(t *testing.T) { - queue := &MemoryQueue{} - queue.Initialize(func(data any) {}, 1, 5) // Small buffer size for test +func TestNewMemoryQueue(t *testing.T) { + workerCount := 3 + bufferSize := 10 + queue := NewMemoryQueue(workerCount, bufferSize) - // Try to push data that should succeed - err := queue.Push("test data") - if err != nil { - t.Errorf("Push should succeed, but got error: %v", err) + if cap(queue.taskChan) != bufferSize { + t.Errorf("Expected buffer size %d, got %d", bufferSize, cap(queue.taskChan)) + } + + 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 TestPushFailWhenFull(t *testing.T) { - 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 +func TestPushAndStop(t *testing.T) { + queue := NewMemoryQueue(1, 5) - queue.Push("data 1") // Fill the buffer - err := queue.Push("data 2") // This should fail + var wg sync.WaitGroup + wg.Add(1) + queue.Push(func() { + time.Sleep(50 * time.Millisecond) // Simulate task delay + wg.Done() + }) - if err == nil { - t.Error("Expected an error when pushing to full queue, but got none") + queue.Stop() + 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 TestPushFailWhenStopped(t *testing.T) { - queue := &MemoryQueue{} - queue.Initialize(func(data any) {}, 1, 1) +func TestPushTimeout(t *testing.T) { + queue := NewMemoryQueue(1, 1) // Small buffer and worker to force full queue - queue.Stop() // Stop the queue before pushing - err := queue.Push("test data") + done := make(chan bool) + go func() { + queue.Push(func() { + time.Sleep(200 * time.Millisecond) // Long enough to cause the second push to timeout + }) + done <- true + }() - if err == nil { - t.Error("Expected an error when pushing to stopped queue, but got none") - } -} - -// 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) + <-done // Ensure first task is pushed + + if err := queue.Push(func() {}); err != nil { + t.Error("Expected timeout error, got nil") } }