|
| 1 | +// |
| 2 | +// Copyright 2026 The InfiniFlow Authors. All Rights Reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +package admin |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + "net/http/httptest" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "ragflow/internal/common" |
| 28 | + "ragflow/internal/engine" |
| 29 | + "ragflow/internal/ingestion/testutil" |
| 30 | + |
| 31 | + "github.com/gin-gonic/gin" |
| 32 | +) |
| 33 | + |
| 34 | +// TestPullMessageFromQueueInitializesSharedConsumer preserves the admin |
| 35 | +// endpoint's ability to pull messages before the ingestor starts. The endpoint |
| 36 | +// must initialize the existing durable consumer rather than creating a |
| 37 | +// dispatcher-specific consumer. |
| 38 | +func TestPullMessageFromQueueInitializesSharedConsumer(t *testing.T) { |
| 39 | + gin.SetMode(gin.TestMode) |
| 40 | + queue := testutil.SetupNatsEngine(t) |
| 41 | + previousQueue := engine.GetMessageQueueEngine() |
| 42 | + engine.SetMessageQueueEngine(queue) |
| 43 | + t.Cleanup(func() { engine.SetMessageQueueEngine(previousQueue) }) |
| 44 | + |
| 45 | + payload, err := json.Marshal(common.TaskMessage{ |
| 46 | + TaskID: "admin-pull-before-ingestor", |
| 47 | + TaskType: common.TaskTypeIngestionTask, |
| 48 | + }) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("marshal task: %v", err) |
| 51 | + } |
| 52 | + if err := queue.PublishTask(common.TaskSubject, payload); err != nil { |
| 53 | + t.Fatalf("publish task: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + recorder := httptest.NewRecorder() |
| 57 | + ctx, _ := gin.CreateTestContext(recorder) |
| 58 | + ctx.Request = httptest.NewRequest( |
| 59 | + http.MethodPost, |
| 60 | + "/", |
| 61 | + bytes.NewBufferString(`{"message_count":1,"ack_policy":"ACK"}`), |
| 62 | + ) |
| 63 | + ctx.Request.Header.Set("Content-Type", "application/json") |
| 64 | + |
| 65 | + (&Handler{}).PullMessageFromQueue(ctx) |
| 66 | + |
| 67 | + var response struct { |
| 68 | + Code int `json:"code"` |
| 69 | + Data []struct { |
| 70 | + ID string `json:"id"` |
| 71 | + Ack string `json:"ack"` |
| 72 | + } `json:"data"` |
| 73 | + } |
| 74 | + if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil { |
| 75 | + t.Fatalf("decode response: %v", err) |
| 76 | + } |
| 77 | + if response.Code != int(common.CodeSuccess) { |
| 78 | + t.Fatalf("response code = %d, want %d; body = %s", response.Code, common.CodeSuccess, recorder.Body.String()) |
| 79 | + } |
| 80 | + if len(response.Data) != 1 || response.Data[0].ID != "admin-pull-before-ingestor" || response.Data[0].Ack != "true" { |
| 81 | + t.Fatalf("pull response = %+v, want acked admin-pull-before-ingestor", response.Data) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestPullMessageFromQueueRejectsOutOfRangeMessageCount(t *testing.T) { |
| 86 | + gin.SetMode(gin.TestMode) |
| 87 | + for _, testCase := range []struct { |
| 88 | + name string |
| 89 | + messageCount int |
| 90 | + }{ |
| 91 | + {name: "negative", messageCount: -1}, |
| 92 | + {name: "zero", messageCount: 0}, |
| 93 | + {name: "above limit", messageCount: 101}, |
| 94 | + } { |
| 95 | + t.Run(testCase.name, func(t *testing.T) { |
| 96 | + recorder := httptest.NewRecorder() |
| 97 | + ctx, _ := gin.CreateTestContext(recorder) |
| 98 | + ctx.Request = httptest.NewRequest( |
| 99 | + http.MethodPost, |
| 100 | + "/", |
| 101 | + bytes.NewBufferString(fmt.Sprintf(`{"message_count":%d,"ack_policy":"ACK"}`, testCase.messageCount)), |
| 102 | + ) |
| 103 | + ctx.Request.Header.Set("Content-Type", "application/json") |
| 104 | + |
| 105 | + (&Handler{}).PullMessageFromQueue(ctx) |
| 106 | + |
| 107 | + var response struct { |
| 108 | + Code int `json:"code"` |
| 109 | + } |
| 110 | + if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil { |
| 111 | + t.Fatalf("decode response: %v", err) |
| 112 | + } |
| 113 | + if response.Code != int(common.CodeBadRequest) { |
| 114 | + t.Fatalf("response code = %d, want %d; body = %s", response.Code, common.CodeBadRequest, recorder.Body.String()) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments