|
| 1 | +// Copyright 2025-2026 Alexander Alten (novatechflow), NovaTechflow (novatechflow.com). |
| 2 | +// This project is supported and financed by Scalytics, Inc. (www.scalytics.io). |
| 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 | +package console |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "encoding/json" |
| 21 | + "errors" |
| 22 | + "sync" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/twmb/franz-go/pkg/kgo" |
| 27 | +) |
| 28 | + |
| 29 | +func TestNewLFSConsumerNoBrokers(t *testing.T) { |
| 30 | + handlers := NewLFSHandlers(LFSConfig{}, nil) |
| 31 | + consumer, err := NewLFSConsumer(context.Background(), LFSConsumerConfig{}, handlers, nil) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("NewLFSConsumer: %v", err) |
| 34 | + } |
| 35 | + if consumer != nil { |
| 36 | + t.Fatal("expected nil consumer when no brokers configured") |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestLFSConsumerStatusInitial(t *testing.T) { |
| 41 | + c := &LFSConsumer{ |
| 42 | + statusMu: sync.RWMutex{}, |
| 43 | + } |
| 44 | + status := c.Status() |
| 45 | + if status.Connected { |
| 46 | + t.Fatal("expected not connected initially") |
| 47 | + } |
| 48 | + if status.LastError != "" { |
| 49 | + t.Fatalf("expected empty error: %q", status.LastError) |
| 50 | + } |
| 51 | + if status.LastPollAt != "" { |
| 52 | + t.Fatalf("expected empty poll time: %q", status.LastPollAt) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestLFSConsumerSetError(t *testing.T) { |
| 57 | + c := &LFSConsumer{ |
| 58 | + statusMu: sync.RWMutex{}, |
| 59 | + } |
| 60 | + c.setError(errors.New("kafka unreachable")) |
| 61 | + status := c.Status() |
| 62 | + if status.LastError != "kafka unreachable" { |
| 63 | + t.Fatalf("error: %q", status.LastError) |
| 64 | + } |
| 65 | + if status.LastErrorAt == "" { |
| 66 | + t.Fatal("expected error time set") |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestLFSConsumerSetErrorNil(t *testing.T) { |
| 71 | + c := &LFSConsumer{ |
| 72 | + statusMu: sync.RWMutex{}, |
| 73 | + } |
| 74 | + c.setError(nil) // should not panic or set anything |
| 75 | + status := c.Status() |
| 76 | + if status.LastError != "" { |
| 77 | + t.Fatalf("expected empty error: %q", status.LastError) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestLFSConsumerSetPollSuccess(t *testing.T) { |
| 82 | + c := &LFSConsumer{ |
| 83 | + statusMu: sync.RWMutex{}, |
| 84 | + } |
| 85 | + // Set an error first |
| 86 | + c.setError(errors.New("temp error")) |
| 87 | + time.Sleep(time.Millisecond) |
| 88 | + // Poll success should clear the error |
| 89 | + c.setPollSuccess() |
| 90 | + status := c.Status() |
| 91 | + if !status.Connected { |
| 92 | + t.Fatal("expected connected after poll success") |
| 93 | + } |
| 94 | + if status.LastError != "" { |
| 95 | + t.Fatalf("expected error cleared: %q", status.LastError) |
| 96 | + } |
| 97 | + if status.LastPollAt == "" { |
| 98 | + t.Fatal("expected poll time set") |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestLFSConsumerProcessRecord(t *testing.T) { |
| 103 | + handlers := NewLFSHandlers(LFSConfig{}, nil) |
| 104 | + c := &LFSConsumer{ |
| 105 | + handlers: handlers, |
| 106 | + logger: nil, |
| 107 | + } |
| 108 | + // Use log.Default() for the consumer logger |
| 109 | + c.logger = handlers.logger |
| 110 | + |
| 111 | + event := LFSEvent{ |
| 112 | + EventType: "upload_completed", |
| 113 | + Topic: "test-topic", |
| 114 | + S3Key: "key-1", |
| 115 | + Size: 512, |
| 116 | + Timestamp: "2026-01-01T00:00:00Z", |
| 117 | + } |
| 118 | + data, _ := json.Marshal(event) |
| 119 | + record := &kgo.Record{Value: data} |
| 120 | + c.processRecord(record) |
| 121 | + |
| 122 | + handlers.mu.RLock() |
| 123 | + defer handlers.mu.RUnlock() |
| 124 | + if handlers.stats.TotalObjects != 1 { |
| 125 | + t.Fatalf("expected 1 object, got %d", handlers.stats.TotalObjects) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestLFSConsumerProcessRecordNil(t *testing.T) { |
| 130 | + handlers := NewLFSHandlers(LFSConfig{}, nil) |
| 131 | + c := &LFSConsumer{ |
| 132 | + handlers: handlers, |
| 133 | + logger: handlers.logger, |
| 134 | + } |
| 135 | + // nil record should not panic |
| 136 | + c.processRecord(nil) |
| 137 | + // empty record should not panic |
| 138 | + c.processRecord(&kgo.Record{Value: nil}) |
| 139 | + c.processRecord(&kgo.Record{Value: []byte{}}) |
| 140 | +} |
| 141 | + |
| 142 | +func TestLFSConsumerProcessRecordInvalidJSON(t *testing.T) { |
| 143 | + handlers := NewLFSHandlers(LFSConfig{}, nil) |
| 144 | + c := &LFSConsumer{ |
| 145 | + handlers: handlers, |
| 146 | + logger: handlers.logger, |
| 147 | + } |
| 148 | + record := &kgo.Record{Value: []byte("not json")} |
| 149 | + c.processRecord(record) // should log error but not panic |
| 150 | + |
| 151 | + handlers.mu.RLock() |
| 152 | + defer handlers.mu.RUnlock() |
| 153 | + if handlers.stats.TotalObjects != 0 { |
| 154 | + t.Fatalf("expected 0 objects after invalid record") |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestLFSConsumerProcessRecordNilHandlers(t *testing.T) { |
| 159 | + c := &LFSConsumer{ |
| 160 | + handlers: nil, |
| 161 | + logger: NewLFSHandlers(LFSConfig{}, nil).logger, |
| 162 | + } |
| 163 | + event := LFSEvent{EventType: "upload_completed", Topic: "t", S3Key: "k", Size: 1} |
| 164 | + data, _ := json.Marshal(event) |
| 165 | + record := &kgo.Record{Value: data} |
| 166 | + c.processRecord(record) // should not panic even with nil handlers |
| 167 | +} |
0 commit comments