Пример #1
0
func setupControllerTest(t *testing.T, qSize int) (*repository.QueueRepository,
	*Controller, *mockTCPConn) {

	repo, err := repository.NewRepository(dir)
	assert.NoError(t, err)

	mockTCPConn := newMockTCPConn()
	controller := NewSession(mockTCPConn, repo)

	q, err := repo.GetQueue("test")
	assert.NoError(t, err)

	for i := 0; i < qSize; i++ {
		q.Enqueue([]byte(strconv.Itoa(i)))
	}

	return repo, controller, mockTCPConn
}
Пример #2
0
// Serve starts the service
func (s *Service) Serve(laddr *net.TCPAddr) {
	defer s.wg.Done()

	log.Println("initializing...")
	var err error
	s.repo, err = repository.NewRepository(s.dataDir)
	log.Println("data directory: ", s.dataDir)
	if err != nil {
		log.Fatal(err)
	}

	listener, err := net.ListenTCP("tcp", laddr)
	if nil != err {
		log.Fatalln(err)
	}
	log.Println("listening on", listener.Addr())

	for {
		select {
		case <-s.ch:
			log.Println("stopping listening on", listener.Addr())
			listener.Close()
			return
		default:
		}
		listener.SetDeadline(time.Now().Add(1e9))
		conn, err := listener.AcceptTCP()
		if nil != err {
			if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
				continue
			}
			log.Println(err)
		}
		s.wg.Add(1)
		go s.handleConnection(conn)
	}
}