| 
									
										
										
										
											2016-07-09 03:38:35 +08:00
										 |  |  | package sftp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 01:58:51 +08:00
										 |  |  | func clientRequestServerPair(t *testing.T) (*Client, *RequestServer) { | 
					
						
							| 
									
										
										
										
											2016-07-09 03:38:35 +08:00
										 |  |  | 	cr, sw := io.Pipe() | 
					
						
							|  |  |  | 	sr, cw := io.Pipe() | 
					
						
							| 
									
										
										
										
											2016-07-12 01:58:51 +08:00
										 |  |  | 	server, err := NewRequestServer(struct { | 
					
						
							| 
									
										
										
										
											2016-07-09 03:38:35 +08:00
										 |  |  | 		io.Reader | 
					
						
							|  |  |  | 		io.WriteCloser | 
					
						
							|  |  |  | 	}{sr, sw}) | 
					
						
							|  |  |  | 	if err != nil { t.Fatal(err) } | 
					
						
							|  |  |  | 	go server.Serve() | 
					
						
							|  |  |  | 	client, err := NewClientPipe(cr, cw) | 
					
						
							|  |  |  | 	if err != nil { t.Fatalf("%+v\n", err) } | 
					
						
							|  |  |  | 	return client, server | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestPsRequestCache(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-07-12 01:58:51 +08:00
										 |  |  | 	_, ps := clientRequestServerPair(t) | 
					
						
							| 
									
										
										
										
											2016-07-09 03:38:35 +08:00
										 |  |  | 	foo := &Request{Filepath: "foo"} | 
					
						
							|  |  |  | 	bar := &Request{Filepath: "bar"} | 
					
						
							|  |  |  | 	ps.nextRequest(foo) | 
					
						
							|  |  |  | 	ps.nextRequest(bar) | 
					
						
							|  |  |  | 	assert.Len(t, ps.openRequests, 2) | 
					
						
							|  |  |  | 	_foo, ok := ps.getRequest("foo") | 
					
						
							|  |  |  | 	assert.Equal(t, foo, _foo) | 
					
						
							|  |  |  | 	assert.True(t, ok) | 
					
						
							|  |  |  | 	_, ok = ps.getRequest("zed") | 
					
						
							|  |  |  | 	assert.False(t, ok) | 
					
						
							|  |  |  | 	ps.closeRequest("foo") | 
					
						
							|  |  |  | 	ps.closeRequest("bar") | 
					
						
							|  |  |  | 	assert.Len(t, ps.openRequests, 0) | 
					
						
							|  |  |  | } |