fix logging by merging channels into reader

This commit is contained in:
zerodoctor
2023-10-05 21:03:50 -05:00
parent 04332e5527
commit 7603b84fa2
3 changed files with 60 additions and 16 deletions

View File

@@ -4,8 +4,51 @@
package engine
import "testing"
import (
"bytes"
"context"
"fmt"
"io"
"testing"
"time"
)
func TestChansToReader(t *testing.T) {
ctx := context.Background()
stdout := make(chan string, 1000)
logs := NewChansReadClose(ctx, stdout)
mockLogs := []string{
"\u0008",
"this is a log 1\n",
"this is a log 2\n",
"this is a log 3\n",
"this is a log 4\n",
"this is a log 5\n",
"this is a log 6\n",
"this is a log 7\n",
"this is a log 8\n",
"this is a log 9\n",
"this is a log 10\n",
"this is a log 11\n",
"this is a log 12\n",
}
go func(mock []string) {
for i := range mock {
time.Sleep(2)
fmt.Printf("sending [mock=%s]\n", mock[i])
stdout <- mock[i]
}
fmt.Println("closing channel...")
logs.Close()
}(mockLogs)
fmt.Println("starting std copy...")
output := bytes.NewBuffer([]byte{})
io.Copy(output, logs)
fmt.Print(output.String())
}