@Test
  public void serverSessionBindRejectedWithInvalidSystemId() throws Exception {
    DefaultSmppServer server0 = createSmppServer();
    server0.start();

    try {
      DefaultSmppClient client0 = new DefaultSmppClient();
      SmppSessionConfiguration sessionConfig0 = createDefaultConfiguration();
      sessionConfig0.setSystemId("TESTID");

      // this should fail (invalid system id)
      try {
        SmppSession session0 = client0.bind(sessionConfig0);
        Assert.fail();
      } catch (SmppBindException e) {
        Assert.assertEquals(SmppConstants.STATUS_INVSYSID, e.getBindResponse().getCommandStatus());
      }

      // give this a little time to catch up
      Thread.sleep(100);

      Assert.assertEquals(0, serverHandler.sessions.size());
      Assert.assertEquals(0, server0.getChannels().size());
    } finally {
      server0.destroy();
    }
  }
  @Test
  public void serverSessionBindRejectedWithInvalidPassword() throws Exception {
    DefaultSmppServer server0 = createSmppServer();
    server0.start();

    try {
      DefaultSmppClient client0 = new DefaultSmppClient();
      SmppSessionConfiguration sessionConfig0 = createDefaultConfiguration();
      sessionConfig0.setSystemId(SYSTEMID);
      sessionConfig0.setPassword("BADPASS");

      // this should fail (invalid password)
      try {
        SmppSession session0 = client0.bind(sessionConfig0);
        Assert.fail();
      } catch (SmppBindException e) {
        Assert.assertEquals(SmppConstants.STATUS_INVPASWD, e.getBindResponse().getCommandStatus());
      }

      Assert.assertEquals(0, serverHandler.sessions.size());
      Assert.assertEquals(0, server0.getChannels().size());
    } finally {
      server0.destroy();
    }
  }
  @Test
  public void serverSessionOK() throws Exception {
    DefaultSmppServer server0 = createSmppServer();
    server0.start();

    try {
      DefaultSmppClient client0 = new DefaultSmppClient();
      SmppSessionConfiguration sessionConfig0 = createDefaultConfiguration();
      // this should actually work
      SmppSession session0 = client0.bind(sessionConfig0);

      Thread.sleep(100);

      SmppServerSession serverSession0 = serverHandler.sessions.iterator().next();
      Assert.assertEquals(1, serverHandler.sessions.size());
      Assert.assertEquals(1, server0.getChannels().size());
      Assert.assertEquals(true, serverSession0.isBound());
      Assert.assertEquals(SmppBindType.TRANSCEIVER, serverSession0.getBindType());
      Assert.assertEquals(SmppSession.Type.SERVER, serverSession0.getLocalType());
      Assert.assertEquals(SmppSession.Type.CLIENT, serverSession0.getRemoteType());

      serverSession0.close();
      Thread.sleep(200);
      Assert.assertEquals(0, serverHandler.sessions.size());
      Assert.assertEquals(0, server0.getChannels().size());
      Assert.assertEquals(false, serverSession0.isBound());
    } finally {
      server0.destroy();
    }
  }
  @Test
  public void serverNotEnoughWorkerThreadsCausesBindTimerToCloseChannel() throws Exception {
    BlockThreadSmppServerHandler serverHandler0 = new BlockThreadSmppServerHandler();
    SmppServerConfiguration configuration = createSmppServerConfiguration();
    // permit up to 0.5 seconds to bind
    configuration.setBindTimeout(500);
    DefaultSmppServer server0 = new DefaultSmppServer(configuration, serverHandler0);
    server0.start();

    try {
      // there is an issue without telling the server how many worker threads
      // to create beforehand with starvation only Runtime.getRuntime().availableProcessors()
      // worker threads are created by default!!! (yikes)
      int workersToStarveWith = Runtime.getRuntime().availableProcessors();

      // initiate bind requests on all sessions we care about -- this should
      // technicaly "starve" the server of worker threads since they'll all
      // be blocked in a Thread.sleep
      for (int i = 0; i < workersToStarveWith; i++) {
        DefaultSmppClient client0 = new DefaultSmppClient();
        SmppSessionConfiguration sessionConfig0 = createDefaultConfiguration();
        sessionConfig0.setName("WorkerTest.Session." + i);
        // don't use default method of binding, connect the socket first
        DefaultSmppSession session0 =
            client0.doOpen(sessionConfig0, new DefaultSmppSessionHandler());
        // try to bind and execute a bind request and wait for a bind response
        BaseBind bindRequest = client0.createBindRequest(sessionConfig0);
        try {
          // just send the request without caring if it succeeds
          session0.sendRequestPdu(bindRequest, 2000, false);
        } catch (SmppChannelException e) {
          System.out.println(e.getMessage());
        }
      }

      // now try to bind normally -- since all previous workers are "starved"
      // this should fail to bind and the socket closed by the "BindTimer"
      DefaultSmppClient client0 = new DefaultSmppClient();
      SmppSessionConfiguration sessionConfig0 = createDefaultConfiguration();
      sessionConfig0.setName("WorkerTestChannelClosed.Session");
      sessionConfig0.setBindTimeout(750);

      try {
        client0.bind(sessionConfig0);
        Assert.fail();
      } catch (SmppTimeoutException e) {
        // the BindTimer should end up closing the connection since the
        // worker thread were "starved"
        logger.debug("Correctly received SmppChannelException during bind");
      }

    } finally {
      Thread.sleep(10500);
      Assert.assertEquals(0, server0.getChannels().size());
      Assert.assertEquals(3, server0.getCounters().getBindTimeouts());
      server0.destroy();
    }
  }