Example #1
0
bool UNICHARSET::load_from_file(FILE *file, bool skip_fragments) {
  LocalFilePointer lfp(file);
  TessResultCallback2<char *, char *, int> *fgets_cb =
      NewPermanentTessCallback(&lfp, &LocalFilePointer::fgets);
  bool success = load_via_fgets(fgets_cb, skip_fragments);
  delete fgets_cb;
  return success;
}
void LeaderFollowerPoolTest::NoAcceptorsTest() {
	StartTrace(LeaderFollowerPoolTest.NoAcceptorsTest);
	LeaderFollowerPool lfp(new TestReactor(this));

	Anything lfpConfig;
	if (t_assertm( !lfp.Init(2, lfpConfig), "no acceptors are configured; init should fail")) {
		t_assertm(lfp.GetPoolSize() == 0, "expected no threads in pool");
		t_assertm(lfp.Start(true, 1000, 11) == -1, "expected Start to fail");
	}
}
void LeaderFollowerPoolTest::NoReactorTest() {
	StartTrace(LeaderFollowerPoolTest.NoReactorTest);
	LeaderFollowerPool lfp(0);

	Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
	Anything lfpConfig;
	lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
	if (t_assertm( !lfp.Init(2, lfpConfig), "no reactor is configured; init should fail")) {
		t_assertm(lfp.GetPoolSize() == 0, "expected no threads in pool");
		t_assertm(lfp.Start(true, 1000, 11) == -1, "expected Start to fail");
	}
}
void LeaderFollowerPoolTest::InvalidAcceptorTest() {
	StartTrace(LeaderFollowerPoolTest.InvalidAcceptorTest);

	LeaderFollowerPool lfp(new TestReactor(this));

	Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port"].AsLong(), 5, 0);
	Anything lfpConfig;
	lfpConfig[String("Accept") << GetConfig()["Testhost"]["port"].AsString()] = (IFAObject *) &ac1;
	if (t_assertm( !lfp.Init(3, lfpConfig), "should not bind to already bound port and needs root privilege anyway")) {
		t_assertm(lfp.GetPoolSize() == 0, "expected no threads in pool");
		t_assertm(lfp.Start(true, 1000, 11) == -1, "expected Start to fail");
	}
}
void LeaderFollowerPoolTest::OneAcceptorTest() {
	StartTrace(LeaderFollowerPoolTest.OneAcceptorTest);

	LeaderFollowerPool lfp(new TestReactor(this));

	Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
	Anything lfpConfig;
	lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
	if (t_assertm( lfp.Init(3, lfpConfig), "some port maybe already bound")) {
		if (t_assert(lfp.Start(true, 1000, 11) == 0 )) {
			ProcessOneEvent();
			lfp.RequestTermination();
			t_assertm(lfp.Join() == 0, "expected Join to succeed");
		}
	}
}
void LeaderFollowerPoolTest::ManyAcceptorsTest() {
	StartTrace(LeaderFollowerPoolTest.ManyAcceptorsTest);
	{
		LeaderFollowerPool lfp(new TestReactor(this));

		Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
		Acceptor ac2(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port2"].AsLong(), 5, 0);
		Acceptor ac3(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port3"].AsLong(), 5, 0);
		Anything lfpConfig;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port2"].AsString()] = (IFAObject *) &ac2;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port3"].AsString()] = (IFAObject *) &ac3;
		if (t_assertm( !lfp.Init(0, lfpConfig), "should not allow LFPool to run without threads")) {
			t_assertm(lfp.GetPoolSize() == 0, "expected no threads in pool");
			t_assertm(lfp.Start(true, 1000, 11) == -1, "expected Start to fail");
		}
	}
	{
		LeaderFollowerPool lfp(new TestReactor(this));

		Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
		Acceptor ac2(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port2"].AsLong(), 5, 0);
		Acceptor ac3(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port3"].AsLong(), 5, 0);
		Anything lfpConfig;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port2"].AsString()] = (IFAObject *) &ac2;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port3"].AsString()] = (IFAObject *) &ac3;
		if (t_assertm( lfp.Init(1, lfpConfig), "some port maybe already bound")) {
			if (t_assert(lfp.Start(false, 1000, 10) == 0 )) {
				ProcessManyEvents();
				lfp.RequestTermination();
				t_assertm(lfp.Join() == 0, "expected Join to succeed");
			}
		}
	}
	{
		LeaderFollowerPool lfp(new TestReactor(this));

		Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
		Acceptor ac2(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port2"].AsLong(), 5, 0);
		Acceptor ac3(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port3"].AsLong(), 5, 0);
		Anything lfpConfig;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port2"].AsString()] = (IFAObject *) &ac2;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port3"].AsString()] = (IFAObject *) &ac3;
		if (t_assertm( lfp.Init(2, lfpConfig), "some port maybe already bound")) {
			if (t_assert(lfp.Start(false, 1000, 10) == 0 )) {
				ProcessManyEvents();
				lfp.RequestTermination();
				t_assertm(lfp.Join() == 0, "expected Join to succeed");
			}
		}
	}
	{
		LeaderFollowerPool lfp(new TestReactor(this));

		Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
		Acceptor ac2(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port2"].AsLong(), 5, 0);
		Acceptor ac3(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port3"].AsLong(), 5, 0);
		Anything lfpConfig;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port2"].AsString()] = (IFAObject *) &ac2;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port3"].AsString()] = (IFAObject *) &ac3;
		if (t_assertm( lfp.Init(3, lfpConfig), "some port maybe already bound")) {
			if (t_assert(lfp.Start(true, 1000, 11) == 0 )) {
				ProcessManyEvents();
				lfp.RequestTermination();
				t_assertm(lfp.Join() == 0, "expected Join to succeed");
			}
		}
	}
	{
		LeaderFollowerPool lfp(new TestReactor(this));

		Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
		Acceptor ac2(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port2"].AsLong(), 5, 0);
		Acceptor ac3(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port3"].AsLong(), 5, 0);
		Anything lfpConfig;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port2"].AsString()] = (IFAObject *) &ac2;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port3"].AsString()] = (IFAObject *) &ac3;
		if (t_assertm( lfp.Init(4, lfpConfig), "some port maybe already bound")) {
			if (t_assert(lfp.Start(true, 1000, 11) == 0 )) {
				ProcessManyEvents();
				lfp.RequestTermination();
				t_assertm(lfp.Join() == 0, "expected Join to succeed");
			}
		}
	}
}