-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- use []byte instead of unnecessary string conversions - make LogManager.Broadcast private - make LogManager.GetHistory public - add tests
- Loading branch information
1 parent
0f133f5
commit 8cf2a38
Showing
4 changed files
with
92 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package proxy | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestLogMonitor(t *testing.T) { | ||
logMonitor := NewLogMonitorWriter(io.Discard) | ||
|
||
// Test subscription | ||
client1 := logMonitor.Subscribe() | ||
client2 := logMonitor.Subscribe() | ||
|
||
defer logMonitor.Unsubscribe(client1) | ||
defer logMonitor.Unsubscribe(client2) | ||
|
||
client1Messages := make([]byte, 0) | ||
client2Messages := make([]byte, 0) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
|
||
go func() { | ||
defer wg.Done() | ||
for { | ||
select { | ||
case data := <-client1: | ||
client1Messages = append(client1Messages, data...) | ||
case data := <-client2: | ||
client2Messages = append(client2Messages, data...) | ||
default: | ||
return | ||
} | ||
} | ||
}() | ||
|
||
logMonitor.Write([]byte("1")) | ||
logMonitor.Write([]byte("2")) | ||
logMonitor.Write([]byte("3")) | ||
|
||
// Wait for the goroutine to finish | ||
wg.Wait() | ||
|
||
// Check the buffer | ||
expectedHistory := "123" | ||
history := string(logMonitor.GetHistory()) | ||
|
||
if history != expectedHistory { | ||
t.Errorf("Expected history: %s, got: %s", expectedHistory, history) | ||
} | ||
|
||
c1Data := string(client1Messages) | ||
if c1Data != expectedHistory { | ||
t.Errorf("Client1 expected %s, got: %s", expectedHistory, c1Data) | ||
} | ||
|
||
c2Data := string(client2Messages) | ||
if c2Data != expectedHistory { | ||
t.Errorf("Client2 expected %s, got: %s", expectedHistory, c2Data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters