forked from golang/go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net/http/httptest: Add NewUnstartedPipeServer
Adds a new server initialization function to create a net.Pipe based listener for a httptest.Server. Fixes golang#14200.
- Loading branch information
1 parent
dcd018b
commit bbbd206
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package httptest | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
"sync" | ||
) | ||
|
||
// A pipeListener is a net.Listener implementation that serves | ||
// in-memory pipes. | ||
type pipeListener struct { | ||
addr pipeAddr | ||
|
||
close sync.Once | ||
done chan struct{} | ||
ch chan net.Conn | ||
} | ||
|
||
func newPipeListener(addr string) *pipeListener { | ||
return &pipeListener{ | ||
addr: pipeAddr(addr), | ||
done: make(chan struct{}), | ||
c: make(chan net.Conn), | ||
} | ||
} | ||
|
||
func (lis *pipeListener) Accept() (net.Conn, error) { | ||
aerr := func(err error) error { | ||
return &net.OpError{ | ||
Op: "accept", | ||
Net: lis.addr.Network(), | ||
Addr: lis.addr, | ||
Err: err, | ||
} | ||
} | ||
select { | ||
case <-lis.done: | ||
return nil, aerr(errors.New("closed")) | ||
case s := <-lis.ch: | ||
return s, nil | ||
} | ||
} | ||
|
||
func (lis *pipeListener) Close() (err error) { | ||
err = errors.New("closed") | ||
lis.close.Do(func() { | ||
close(lis.done) | ||
err = nil | ||
}) | ||
return | ||
} | ||
|
||
func (lis *pipeListener) Addr() net.Addr { | ||
return pipeAddr(lis.addr) | ||
} | ||
|
||
// Dial "dials" the listener, creating a pipe. It returns the client's | ||
// end of the connection and causes a waiting Accept call to return | ||
// the server's end. | ||
func (lis *pipeListener) DialContext(ctx context.Context, network, addr string) (net.Conn, error) { | ||
derr := func(err error) error { | ||
return &net.OpError{ | ||
Op: "dial", | ||
Net: lis.addr.Network(), | ||
Err: err, | ||
} | ||
} | ||
s, c := net.Pipe() | ||
select { | ||
case <-ctx.Done(): | ||
return nil, derr(ctx.Err()) | ||
case lis.ch <- s: | ||
return c, nil | ||
case <-lis.done: | ||
return nil, derr(errors.New("closed")) | ||
} | ||
return c, nil | ||
} | ||
|
||
type pipeAddr string | ||
|
||
func (pipeAddr) Network() string { | ||
return "pipe" | ||
} | ||
|
||
func (addr pipeAddr) String() string { | ||
return string(addr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters