program terminates at end of input file properly

master
Jordan Orelli 11 years ago
parent 8a5c9ba3f2
commit 04e9541b77

@ -48,7 +48,6 @@ func main() {
if options.password != "" { if options.password != "" {
auth(options.password).Write(conn) auth(options.password).Write(conn)
// fmt.Fprintf(conn, "*2\r\n$4\r\nauth\r\n$%d\r\n%s\r\n", len(options.password), options.password)
v, err := readValue(conn) v, err := readValue(conn)
if err != nil { if err != nil {
fmt.Printf("unable to auth: %v\n", err) fmt.Printf("unable to auth: %v\n", err)
@ -67,49 +66,30 @@ func main() {
} }
defer infile.Close() defer infile.Close()
c, e := make(chan value), make(chan error) c := make(chan maybe)
sent := make(chan value) sent := make(chan value)
go streamValues(infile, c, e) go streamValues(infile, c)
go func() { go func() {
for { defer close(sent)
select { for r := range c {
case v, ok := <-c: if r.ok() {
if !ok { r.val().Write(conn)
return sent <- r.val()
} } else {
v.Write(conn) // this bad
sent <- v fmt.Println(r.err())
case err, ok := <-e:
if !ok {
return
}
fmt.Println(err)
} }
} }
}() }()
type pair struct { responses := make(chan maybe)
request value go streamValues(conn, responses)
response value for request := range sent {
} response := <-responses
if response.ok() {
cc, ee := make(chan value), make(chan error) fmt.Fprintf(os.Stdout, "%v +> %v\n", request, response.val())
go streamValues(conn, cc, ee) } else {
ReadResponses: fmt.Fprintf(os.Stderr, "%v -> %v\n", request, response.val())
for {
select {
case response, ok := <-cc:
if !ok {
break ReadResponses
}
request := <-sent
fmt.Println(pair{request, response})
case err, ok := <-ee:
if !ok {
break ReadResponses
}
request := <-sent
fmt.Printf("fuck %v %v\n", request, err)
} }
} }
} }

@ -19,6 +19,23 @@ type value interface {
Write(io.Writer) (int, error) Write(io.Writer) (int, error)
} }
type maybe struct {
value
error
}
func (m maybe) err() error {
return m.error
}
func (m maybe) ok() bool {
return m.error == nil
}
func (m maybe) val() value {
return m.value
}
func auth(password string) value { func auth(password string) value {
return Array{BulkString("auth"), BulkString(password)} return Array{BulkString("auth"), BulkString(password)}
} }
@ -31,9 +48,8 @@ func isOK(v value) bool {
return vv == "OK" return vv == "OK"
} }
func streamValues(r io.Reader, c chan value, e chan error) { func streamValues(r io.Reader, c chan maybe) {
defer close(c) defer close(c)
defer close(e)
r = bufio.NewReader(r) r = bufio.NewReader(r)
for { for {
@ -42,9 +58,9 @@ func streamValues(r io.Reader, c chan value, e chan error) {
case io.EOF: case io.EOF:
return return
case nil: case nil:
c <- v c <- maybe{value: v}
default: default:
e <- err c <- maybe{error: err}
} }
} }
} }

@ -132,24 +132,16 @@ func (s streamTest) run(t *testing.T) {
out[i-1] = s[i].(value) out[i-1] = s[i].(value)
} }
c, e := make(chan value), make(chan error) c := make(chan maybe)
go streamValues(strings.NewReader(in), c, e) go streamValues(strings.NewReader(in), c)
var count int var count int
for { for v := range c {
select { if v.ok() {
case v, ok := <-c: if !eq(out[count], v.val()) {
if !ok { t.Errorf("expected %q, got %q", out[count], v.val())
return
}
if !eq(out[count], v) {
t.Errorf("expected %q, got %q", out[count], v)
}
case err, ok := <-e:
if !ok {
return
} }
t.Error(err) } else {
return t.Error(v.err())
} }
count++ count++
} }

Loading…
Cancel
Save