<commit_before>package mgo

import (
	. "launchpad.net/gocheck"
	"net"
)

type ServerSuite struct {
}

var _ = Suite(&ServerSuite{})

func (s *ServerSuite) TestCloseDuringConnect(c *C) {
	var server *mongoServer
	closeDial := func(addr net.Addr) (net.Conn, error) {
		// The first call to this will be during newServer, and
		// server will be nil. Once it returns, the second call
		// will be with a valid server, and we will Close it.
		if server != nil {
			server.Close()
		}
		return net.DialTCP("tcp", nil, addr.(*net.TCPAddr))
	}
	localhostServer, err := net.Listen("tcp", "localhost:0")
	c.Assert(err, IsNil)
	defer localhostServer.Close()
	go func() {
		// Accept a connection but don't do anything with it
		for {
			conn, err := localhostServer.Accept()
                        logf("got connection: %v", conn)
			if err != nil {
				return
			}
			conn.Close()
		}
	}()
	tcpaddr := localhostServer.Addr().(*net.TCPAddr)
	server = newServer(tcpaddr.String(), tcpaddr, make(chan bool), closeDial)
	c.Assert(server, NotNil)
	// It is possible for newServer to ping the remote host, and the
	// request returns immediately (because writing to the socket fails).
	// In which case pinger(loop=false) puts the closed socket back into
	// unused. We have to wait until readLoop gets a EOF and removes the
	// socket from the queue.
	for {
		server.RLock()
		count := len(server.unusedSockets)
		server.RUnlock()
		if count == 0 {
			break
		}
                logf("server had %d unused sockets waiting for 0", count)
	}
	conn, _, err := server.AcquireSocket(0)
	c.Check(err, Equals, errServerClosed)
	c.Assert(conn, IsNil)
}<commit_msg>go fmt<commit_after>package mgo

import (
	. "launchpad.net/gocheck"
	"net"
)

type ServerSuite struct {
}

var _ = Suite(&ServerSuite{})

func (s *ServerSuite) TestCloseDuringConnect(c *C) {
	var server *mongoServer
	closeDial := func(addr net.Addr) (net.Conn, error) {
		// The first call to this will be during newServer, and
		// server will be nil. Once it returns, the second call
		// will be with a valid server, and we will Close it.
		if server != nil {
			server.Close()
		}
		return net.DialTCP("tcp", nil, addr.(*net.TCPAddr))
	}
	localhostServer, err := net.Listen("tcp", "localhost:0")
	c.Assert(err, IsNil)
	defer localhostServer.Close()
	go func() {
		// Accept a connection but don't do anything with it
		for {
			conn, err := localhostServer.Accept()
			logf("got connection: %v", conn)
			if err != nil {
				return
			}
			conn.Close()
		}
	}()
	tcpaddr := localhostServer.Addr().(*net.TCPAddr)
	server = newServer(tcpaddr.String(), tcpaddr, make(chan bool), closeDial)
	c.Assert(server, NotNil)
	// It is possible for newServer to ping the remote host, and the
	// request returns immediately (because writing to the socket fails).
	// In which case pinger(loop=false) puts the closed socket back into
	// unused. We have to wait until readLoop gets a EOF and removes the
	// socket from the queue.
	for {
		server.RLock()
		count := len(server.unusedSockets)
		server.RUnlock()
		if count == 0 {
			break
		}
		logf("server had %d unused sockets waiting for 0", count)
	}
	conn, _, err := server.AcquireSocket(0)
	c.Check(err, Equals, errServerClosed)
	c.Assert(conn, IsNil)
}
{'fraction_non_alphanumeric': 0.12523133867982728, 'fraction_numerical': 0.0024676125848241827, 'mean_word_length': 3.400271370420624, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6109150', 'n_tokens_mistral': 1153, 'n_tokens_neox': 1064, 'n_words': 357}