Commit 68e51de0 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/rpc: MuxConn.NextId properly increments

parent 2ac629c9
...@@ -72,7 +72,7 @@ func (m *MuxConn) Accept(id uint32) (io.ReadWriteCloser, error) { ...@@ -72,7 +72,7 @@ func (m *MuxConn) Accept(id uint32) (io.ReadWriteCloser, error) {
stream.mu.Lock() stream.mu.Lock()
if stream.state != streamStateSynRecv && stream.state != streamStateClosed { if stream.state != streamStateSynRecv && stream.state != streamStateClosed {
stream.mu.Unlock() stream.mu.Unlock()
return nil, fmt.Errorf("Stream already open in bad state: %d", stream.state) return nil, fmt.Errorf("Stream %d already open in bad state: %d", id, stream.state)
} }
if stream.state == streamStateSynRecv { if stream.state == streamStateSynRecv {
...@@ -124,7 +124,7 @@ func (m *MuxConn) Dial(id uint32) (io.ReadWriteCloser, error) { ...@@ -124,7 +124,7 @@ func (m *MuxConn) Dial(id uint32) (io.ReadWriteCloser, error) {
stream.mu.Lock() stream.mu.Lock()
if stream.state != streamStateClosed { if stream.state != streamStateClosed {
stream.mu.Unlock() stream.mu.Unlock()
return nil, fmt.Errorf("Stream already open in bad state: %d", stream.state) return nil, fmt.Errorf("Stream %d already open in bad state: %d", id, stream.state)
} }
// Open a connection // Open a connection
...@@ -157,11 +157,11 @@ func (m *MuxConn) NextId() uint32 { ...@@ -157,11 +157,11 @@ func (m *MuxConn) NextId() uint32 {
defer m.mu.Unlock() defer m.mu.Unlock()
for { for {
if _, ok := m.streams[m.curId]; !ok { result := m.curId
return m.curId
}
m.curId++ m.curId++
if _, ok := m.streams[result]; !ok {
return result
}
} }
} }
......
...@@ -159,3 +159,16 @@ func TestMuxConn_serverClosesStreams(t *testing.T) { ...@@ -159,3 +159,16 @@ func TestMuxConn_serverClosesStreams(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
} }
func TestMuxConnNextId(t *testing.T) {
client, server := testMux(t)
defer client.Close()
defer server.Close()
a := client.NextId()
b := client.NextId()
if a != 0 || b != 1 {
t.Fatalf("IDs should increment")
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment