Add graceful shutdown to TestServer
This commit is contained in:
		
							parent
							
								
									9c40914bba
								
							
						
					
					
						commit
						4a4b5e1303
					
				
					 1 changed files with 18 additions and 6 deletions
				
			
		|  | @ -37,16 +37,28 @@ export default class TestServer { | ||||||
|     return `http://${addr.address}:${addr.port}${path}` |     return `http://${addr.address}:${addr.port}${path}` | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   listen() { |   start(): Promise<void>  { | ||||||
|  |     if (this.http) { | ||||||
|  |       throw new Error('http server already started'); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     this.http = http.createServer(this.app) |     this.http = http.createServer(this.app) | ||||||
|     this.http.listen(this.port, this.address); |     return new Promise((resolve) => { | ||||||
|  |       this.http!!.listen(this.port, this.address, () => { | ||||||
|  |         resolve(); | ||||||
|  |       }) | ||||||
|  |     }); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   close(): void { |   stop(): Promise<void> { | ||||||
|     if (!this.http) { |     if (!this.http) { | ||||||
|       return; |       return Promise.resolve(); | ||||||
|     } |     } | ||||||
|     this.http.close(); |     return new Promise((resolve) => { | ||||||
|     this.http = undefined; |       this.http!!.close(() => { | ||||||
|  |         this.http = undefined; | ||||||
|  |         resolve(); | ||||||
|  |       }); | ||||||
|  |     }) | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
		Reference in a new issue