never block when calling the onHandshakeComplete callback

This commit is contained in:
Marten Seemann 2018-12-28 23:32:41 +07:00
parent 8ac77be934
commit 1301610a54
2 changed files with 34 additions and 2 deletions

View file

@ -149,7 +149,7 @@ func listen(conn net.PacketConn, tlsConf *tls.Config, config *Config) (*server,
tlsConf: tlsConf, tlsConf: tlsConf,
config: config, config: config,
sessionHandler: sessionHandler, sessionHandler: sessionHandler,
sessionQueue: make(chan Session, 5), sessionQueue: make(chan Session),
errorChan: make(chan struct{}), errorChan: make(chan struct{}),
newSession: newSession, newSession: newSession,
logger: utils.DefaultLogger.WithPrefix("server"), logger: utils.DefaultLogger.WithPrefix("server"),
@ -164,7 +164,7 @@ func listen(conn net.PacketConn, tlsConf *tls.Config, config *Config) (*server,
func (s *server) setup() error { func (s *server) setup() error {
s.sessionRunner = &runner{ s.sessionRunner = &runner{
onHandshakeCompleteImpl: func(sess Session) { s.sessionQueue <- sess }, onHandshakeCompleteImpl: func(sess Session) { go func() { s.sessionQueue <- sess }() },
retireConnectionIDImpl: s.sessionHandler.Retire, retireConnectionIDImpl: s.sessionHandler.Retire,
removeConnectionIDImpl: s.sessionHandler.Remove, removeConnectionIDImpl: s.sessionHandler.Remove,
} }

View file

@ -390,6 +390,38 @@ var _ = Describe("Server", func() {
close(completeHandshake) close(completeHandshake)
Eventually(done).Should(BeClosed()) Eventually(done).Should(BeClosed())
}) })
It("never blocks when calling the onHandshakeComplete callback", func() {
const num = 50
done := make(chan struct{}, num)
serv.newSession = func(
_ connection,
runner sessionRunner,
_ protocol.ConnectionID,
_ protocol.ConnectionID,
_ protocol.ConnectionID,
_ *Config,
_ *tls.Config,
_ *handshake.TransportParameters,
_ utils.Logger,
_ protocol.VersionNumber,
) (quicSession, error) {
sess := NewMockQuicSession(mockCtrl)
runner.onHandshakeComplete(sess)
sess.EXPECT().run().Do(func() {})
done <- struct{}{}
return sess, nil
}
go func() {
for i := 0; i < num; i++ {
_, err := serv.createNewSession(&net.UDPAddr{}, nil, nil, nil, nil, protocol.VersionWhatever)
Expect(err).ToNot(HaveOccurred())
}
}()
Eventually(done).Should(HaveLen(num))
})
}) })
}) })