| 
									
										
										
										
											2017-03-01 10:41:54 -05:00
										 |  |  | package proxy | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2017-06-02 17:19:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"google.golang.org/grpc/grpclog" | 
					
						
							| 
									
										
										
										
											2017-03-01 10:41:54 -05:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func pool() []*UpstreamHost { | 
					
						
							|  |  |  | 	return []*UpstreamHost{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Name: "localhost:10053", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Name: "localhost:10054", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestStartupShutdown(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-06-02 17:19:40 +01:00
										 |  |  | 	grpclog.SetLogger(discard{}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 10:41:54 -05:00
										 |  |  | 	upstream := &staticUpstream{ | 
					
						
							|  |  |  | 		from:        ".", | 
					
						
							|  |  |  | 		Hosts:       pool(), | 
					
						
							|  |  |  | 		Policy:      &Random{}, | 
					
						
							|  |  |  | 		Spray:       nil, | 
					
						
							|  |  |  | 		FailTimeout: 10 * time.Second, | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 		Future:      60 * time.Second, | 
					
						
							| 
									
										
										
										
											2017-03-01 10:41:54 -05:00
										 |  |  | 		MaxFails:    1, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	g := newGrpcClient(nil, upstream) | 
					
						
							|  |  |  | 	upstream.ex = g | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-02 17:19:40 +01:00
										 |  |  | 	p := &Proxy{} | 
					
						
							| 
									
										
										
										
											2017-03-01 10:41:54 -05:00
										 |  |  | 	p.Upstreams = &[]Upstream{upstream} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err := g.OnStartup(p) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error starting grpc client exchanger: %s", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(g.clients) != len(pool()) { | 
					
						
							|  |  |  | 		t.Errorf("Expected %d grpc clients but found %d", len(pool()), len(g.clients)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err = g.OnShutdown(p) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Error stopping grpc client exchanger: %s", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(g.clients) != 0 { | 
					
						
							|  |  |  | 		t.Errorf("Shutdown didn't remove clients, found %d", len(g.clients)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(g.conns) != 0 { | 
					
						
							|  |  |  | 		t.Errorf("Shutdown didn't remove conns, found %d", len(g.conns)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-06-02 17:19:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // discard is a Logger that outputs nothing. | 
					
						
							|  |  |  | type discard struct{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (d discard) Fatal(args ...interface{})                 {} | 
					
						
							|  |  |  | func (d discard) Fatalf(format string, args ...interface{}) {} | 
					
						
							|  |  |  | func (d discard) Fatalln(args ...interface{})               {} | 
					
						
							|  |  |  | func (d discard) Print(args ...interface{})                 {} | 
					
						
							|  |  |  | func (d discard) Printf(format string, args ...interface{}) {} | 
					
						
							|  |  |  | func (d discard) Println(args ...interface{})               {} |