Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added unlimited probed writer option #82

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/fatih/color v1.15.0
github.com/kopoli/go-terminal-size v0.0.0-20170219200355-5c97524c8b54
github.com/mattn/go-isatty v0.0.19
github.com/sha1n/gommons v0.0.14
github.com/sha1n/gommons v0.0.15
github.com/stretchr/testify v1.8.4
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sha1n/gommons v0.0.14 h1:LsZeG85USg/vdOdUQonwRVl5kLe3Ryv0pbqn9r48yiU=
github.com/sha1n/gommons v0.0.14/go.mod h1:Qf66Mv+Lrocn720WwqivcpbhVSyQA4u/N/mPo3F3w00=
github.com/sha1n/gommons v0.0.15 h1:YqCjOX3dFkdehrLMGAzBIXpAmNJXvIHc/uN63lm9gO0=
github.com/sha1n/gommons v0.0.15/go.mod h1:Qf66Mv+Lrocn720WwqivcpbhVSyQA4u/N/mPo3F3w00=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
27 changes: 14 additions & 13 deletions spinner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/sha1n/gommons/pkg/io"
"github.com/stretchr/testify/assert"
)

Expand All @@ -16,30 +17,30 @@ const (
)

func TestSpinnerCharSequence(t *testing.T) {
emulatedStdout := NewThreadSafeBuffer()
probedWriter := io.NewProbedWriter(new(bytes.Buffer), 1024*1024*1024)

spinner := NewSpinner(emulatedStdout, "", interval, DefaultSpinnerFormatter())
spinner := NewSpinner(probedWriter, "", interval, DefaultSpinnerFormatter())
cancel, err := spinner.Start()
defer cancel()

assert.NoError(t, err)
assert.NotNil(t, cancel)

assertSpinnerCharSequence(t, emulatedStdout)
assertSpinnerCharSequence(t, probedWriter)
}

func TestSpinnerCancellation(t *testing.T) {
emulatedStdout := NewThreadSafeBuffer()
probedWriter := io.NewProbedWriter(new(bytes.Buffer), 1024*1024*1024)

spin := NewSpinner(emulatedStdout, "", interval, DefaultSpinnerFormatter())
spin := NewSpinner(probedWriter, "", interval, DefaultSpinnerFormatter())
cancel, err := spin.Start()

assert.NoError(t, err)
assert.NotNil(t, cancel)
assertSpinnerCharSequence(t, emulatedStdout)
assertSpinnerCharSequence(t, probedWriter)

cancel()
assertStoppedEventually(t, emulatedStdout, spin.(*spinner))
assertStoppedEventually(t, probedWriter, spin.(*spinner))
}

func TestSpinnerStartAlreadyRunning(t *testing.T) {
Expand Down Expand Up @@ -135,7 +136,7 @@ func bufferContains(outBuffer *bytes.Buffer, expected string) func() bool {
}
}

func assertStoppedEventually(t *testing.T, outBuffer *ThreadSafeBufferWriter, spinner *spinner) {
func assertStoppedEventually(t *testing.T, probedWriter *io.ProbedWriter, spinner *spinner) {
assert.Eventually(
t,
func() bool { return !spinner.isActiveSafe() },
Expand All @@ -147,24 +148,24 @@ func assertStoppedEventually(t *testing.T, outBuffer *ThreadSafeBufferWriter, sp
assert.Eventually(
t,
func() bool {
outBuffer.Reset()
probedWriter.Reset()
time.Sleep(spinner.interval * 2)
return outBuffer.Len() == 0
return len(probedWriter.Bytes()) == 0

},
timeout,
spinner.interval,
"expected no more output from spinner",
)
}

func assertSpinnerCharSequence(t *testing.T, outBuffer *ThreadSafeBufferWriter) {
func assertSpinnerCharSequence(t *testing.T, probedWriter *io.ProbedWriter) {
charSeq := DefaultSpinnerCharSeq()
expectedCharSequence := strings.Join(charSeq, "")
var read string = ""

for {
read = stripControlCharacters(outBuffer.String())
read = stripControlCharacters(probedWriter.String())
if len(read) >= len(expectedCharSequence)*2 {
break
}
Expand Down
26 changes: 13 additions & 13 deletions thread_safe_buffer.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package termite

import (
"bytes"
"sync"
"bytes"
"sync"
)

type ThreadSafeBufferWriter struct {
buf *bytes.Buffer
mutex sync.Mutex
buf *bytes.Buffer
mutex sync.Mutex
}

func NewThreadSafeBuffer() *ThreadSafeBufferWriter {
return &ThreadSafeBufferWriter{
buf: new(bytes.Buffer),
}
return &ThreadSafeBufferWriter{
buf: new(bytes.Buffer),
}
}

func (b *ThreadSafeBufferWriter) Write(p []byte) (n int, err error) {
b.mutex.Lock()
defer b.mutex.Unlock()
return b.buf.Write(p)
b.mutex.Lock()
defer b.mutex.Unlock()
return b.buf.Write(p)
}

func (b *ThreadSafeBufferWriter) String() string {
b.mutex.Lock()
defer b.mutex.Unlock()
return b.buf.String()
b.mutex.Lock()
defer b.mutex.Unlock()
return b.buf.String()
}

func (b *ThreadSafeBufferWriter) Len() int {
Expand Down
Loading