@@ -3,6 +3,8 @@ package client
33import (
44 "bytes"
55 "encoding/json"
6+ "errors"
7+ "net/http"
68 "testing"
79 "time"
810
@@ -307,3 +309,138 @@ func TestLmstfyClient_DeleteDeadLetter(t *testing.T) {
307309 t .Fatal ("delete deadletter failed" )
308310 }
309311}
312+
313+ func TestLmstfyClient_ValidateConsumeTimeout (t * testing.T ) {
314+ // Test with default HTTP client (600 seconds timeout)
315+ cli := NewLmstfyClient (Host , Port , Namespace , Token )
316+
317+ // Test valid timeout (should return nil)
318+ err := cli .validateConsumeTimeout (300 )
319+ if err != nil {
320+ t .Fatalf ("Expected nil for valid timeout, got: %v" , err )
321+ }
322+
323+ // Test zero timeout (should return nil)
324+ err = cli .validateConsumeTimeout (0 )
325+ if err != nil {
326+ t .Fatalf ("Expected nil for zero timeout, got: %v" , err )
327+ }
328+
329+ // Test invalid timeout (should return error)
330+ err = cli .validateConsumeTimeout (600 )
331+ if err == nil {
332+ t .Fatal ("Expected error for timeout >= HTTP timeout" )
333+ }
334+ expectedMsg := "consume timeout (600 seconds) must be less than HTTP client timeout (600 seconds)"
335+ var apiErr * APIError
336+ if ! errors .As (err , & apiErr ) || apiErr .Reason != expectedMsg {
337+ t .Fatalf ("Expected error message '%s', got '%v'" , expectedMsg , err )
338+ }
339+ }
340+
341+ func TestLmstfyClient_ConsumeWithTimeoutValidation (t * testing.T ) {
342+ // Test with custom HTTP client with short timeout
343+ shortHTTPClient := & http.Client {
344+ Timeout : 5 * time .Second ,
345+ }
346+ cli := NewLmstfyWithClient (shortHTTPClient , Host , Port , Namespace , Token )
347+
348+ // Test consume with valid timeout
349+ job , err := cli .Consume ("test-timeout-validation" , 10 , 3 )
350+ if err != nil {
351+ t .Fatalf ("Consume should succeed with valid timeout: %v" , err )
352+ }
353+ if job != nil {
354+ cli .Ack ("test-timeout-validation" , job .ID )
355+ }
356+
357+ // Test consume with invalid timeout (should fail)
358+ _ , err = cli .Consume ("test-timeout-validation" , 10 , 6 )
359+ if err == nil {
360+ t .Fatal ("Consume should fail with timeout >= HTTP timeout" )
361+ }
362+ expectedMsg := "consume timeout (6 seconds) must be less than HTTP client timeout (5 seconds)"
363+ var apiErr * APIError
364+ if ! errors .As (err , & apiErr ) || apiErr .Reason != expectedMsg {
365+ t .Fatalf ("Expected error message '%s', got '%v'" , expectedMsg , err )
366+ }
367+ }
368+
369+ func TestLmstfyClient_BatchConsumeWithTimeoutValidation (t * testing.T ) {
370+ // Test with custom HTTP client with short timeout
371+ shortHTTPClient := & http.Client {
372+ Timeout : 5 * time .Second ,
373+ }
374+ cli := NewLmstfyWithClient (shortHTTPClient , Host , Port , Namespace , Token )
375+
376+ // Test batch consume with valid timeout
377+ queues := []string {"test-batch-timeout-validation" }
378+ jobs , err := cli .BatchConsume (queues , 3 , 10 , 3 )
379+ if err != nil {
380+ t .Fatalf ("BatchConsume should succeed with valid timeout: %v" , err )
381+ }
382+
383+ // Ack any jobs that were returned
384+ for _ , job := range jobs {
385+ cli .Ack ("test-batch-timeout-validation" , job .ID )
386+ }
387+
388+ // Test batch consume with invalid timeout (should fail)
389+ _ , err = cli .BatchConsume (queues , 3 , 10 , 6 )
390+ if err == nil {
391+ t .Fatal ("BatchConsume should fail with timeout >= HTTP timeout" )
392+ }
393+ expectedMsg := "consume timeout (6 seconds) must be less than HTTP client timeout (5 seconds)"
394+ var apiErr * APIError
395+ if ! errors .As (err , & apiErr ) || apiErr .Reason != expectedMsg {
396+ t .Fatalf ("Expected error message '%s', got '%v'" , expectedMsg , err )
397+ }
398+ }
399+
400+ func TestLmstfyClient_ConsumeFromQueuesWithTimeoutValidation (t * testing.T ) {
401+ // Test with custom HTTP client with short timeout
402+ shortHTTPClient := & http.Client {
403+ Timeout : 5 * time .Second ,
404+ }
405+ cli := NewLmstfyWithClient (shortHTTPClient , Host , Port , Namespace , Token )
406+
407+ // Test consume from queues with valid timeout
408+ job , err := cli .ConsumeFromQueues (10 , 3 , "test-multi-queue-timeout-validation" )
409+ if err != nil {
410+ t .Fatalf ("ConsumeFromQueues should succeed with valid timeout: %v" , err )
411+ }
412+ if job != nil {
413+ cli .Ack ("test-multi-queue-timeout-validation" , job .ID )
414+ }
415+
416+ // Test consume from queues with invalid timeout (should fail)
417+ _ , err = cli .ConsumeFromQueues (10 , 6 , "test-multi-queue-timeout-validation" )
418+ if err == nil {
419+ t .Fatal ("ConsumeFromQueues should fail with timeout >= HTTP timeout" )
420+ }
421+ expectedMsg := "consume timeout (6 seconds) must be less than HTTP client timeout (5 seconds)"
422+ var apiErr * APIError
423+ if ! errors .As (err , & apiErr ) || apiErr .Reason != expectedMsg {
424+ t .Fatalf ("Expected error message '%s', got '%v'" , expectedMsg , err )
425+ }
426+ }
427+
428+ func TestLmstfyClient_GetHTTPTimeout (t * testing.T ) {
429+ // Test with default HTTP client
430+ cli := NewLmstfyClient (Host , Port , Namespace , Token )
431+ timeout := cli .getHTTPTimeout ()
432+ if timeout != 600 * time .Second {
433+ t .Fatalf ("Expected 600 seconds timeout for default client, got %v" , timeout )
434+ }
435+
436+ // Test with custom HTTP client
437+ customTimeout := 30 * time .Second
438+ customClient := & http.Client {
439+ Timeout : customTimeout ,
440+ }
441+ cli2 := NewLmstfyWithClient (customClient , Host , Port , Namespace , Token )
442+ timeout = cli2 .getHTTPTimeout ()
443+ if timeout != customTimeout {
444+ t .Fatalf ("Expected %v timeout for custom client, got %v" , customTimeout , timeout )
445+ }
446+ }
0 commit comments