Skip to content

Commit

Permalink
fix: drain error before finalize command
Browse files Browse the repository at this point in the history
  • Loading branch information
galihrivanto committed Feb 17, 2021
1 parent a96462f commit 23eab15
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
33 changes: 21 additions & 12 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,22 +247,31 @@ func (p *Proxy) sendCommand(b []byte, waitPrompt ...bool) ([]byte, error) {
}

waitLoop:
for {
// wait until received prompt
bytesOut := <-p.stdout
debug(string(bytesOut))
parts := bytes.Split(bytesOut, []byte("\n"))
for _, part := range parts {
if isPrompt(part) {
break waitLoop
}
}

output = append(output, bytesOut...)
}

// drain error channel
errLoop:
for {
select {
case bytesErr := <-p.stderr:
debug(string(bytesErr))
err = fmt.Errorf("%s", string(bytesErr))
break waitLoop
case bytesOut := <-p.stdout:
debug(string(bytesOut))
parts := bytes.Split(bytesOut, []byte("\n"))
for _, part := range parts {
if isPrompt(part) {
break waitLoop
}
if len(bytesErr) > 0 {
debug(string(bytesErr))
err = fmt.Errorf("%s", string(bytesErr))
}

output = append(output, bytesOut...)
default:
break errLoop
}
}

Expand Down
36 changes: 30 additions & 6 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ package inkscape

import (
"fmt"
"os"
"testing"
)

func TestConcurrent(t *testing.T) {
tempFiles := make([]string, 0)
// defer func() {
// for _, t := range tempFiles {
// os.Remove(t)
// }
// }()
defer func() {
for _, t := range tempFiles {
os.Remove(t)
}
}()

proxy := NewProxy(Verbose(true))
proxy.Run()
err := proxy.Run()
if err != nil {
t.Error(err)
t.FailNow()
}
defer proxy.Close()

for i := 0; i < 10; i++ {
temp := fmt.Sprintf("%d.pdf", i)
Expand All @@ -27,3 +33,21 @@ func TestConcurrent(t *testing.T) {
}()
}
}

func TestOpenFail(t *testing.T) {
proxy := NewProxy(Verbose(true))
err := proxy.Run()
if err != nil {
t.Error(err)
t.FailNow()
}
defer proxy.Close()

res, err := proxy.RawCommands("file-open:notexists.svg")
if err == nil {
t.Error("should fail", string(res))
t.FailNow()
}

t.Log(string(res))
}

0 comments on commit 23eab15

Please sign in to comment.