GIF89a=( õ' 7IAXKgNgYvYx\%wh…hŽth%ˆs%—x¨}9®Œ©€&©‰%¶†(¹–.¹5·œD¹&Çš)ÇŸ5ǘ;Í£*È¡&Õ²)ׯ7×µ<Ñ»4ï°3ø‘HÖ§KͯT÷¨Yÿšqÿ»qÿÔFØ !ù ' !ÿ NETSCAPE2.0 , =( þÀ“pH,È¤rÉl:ŸÐ¨tJ­Z¯Ø¬vËíz¿à°xL.›Ïè´zÍn»ßð¸|N¯Ûïø¼~Ïïûÿ€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§gª«ªE¯°¨¬ª±²Œ¹º¹E¾­”´ÂB¶¯ §Åȸ»ÑD¾¿Á•ÄÅ®° ÝH¾ÒLÀÆDÙ«D¶BÝïðÀ¾DÑÑÔTÌÍíH òGö¨A RÎڐ |¥ ٭&ºìE8œ¹kGÔAÞpx­a¶­ã R2XB®åE8I€Õ6Xî:vT)äžþÀq¦è³¥ì仕F~%xñ  4#ZÔ‰O|-4Bs‘X:= QÉ œš lºÒyXJŠGȦ|s hÏíK–3l7·B|¥$'7Jީܪ‰‡àá”Dæn=Pƒ ¤Òëí‰`䌨ljóá¯Éüv>á–Á¼5 ½.69ûϸd«­ºÀûnlv©‹ªîf{¬ÜãPbŸ  l5‘ޝpß ´ ˜3aÅùäI«O’ý·‘áÞ‡˜¾Æ‚ÙÏiÇÿ‹Àƒ #öó)pâš Þ½ ‘Ý{ó)vmÞü%D~ 6f s}ŃƒDØW Eþ`‡þ À…L8xá†ç˜{)x`X/> Ì}mø‚–RØ‘*|`D=‚Ø_ ^ð5 !_…'aä“OÚ—7âcð`D”Cx`ÝÂ¥ä‹éY¹—F¼¤¥Š?¡Õ™ n@`} lď’ÄÉ@4>ñd œ à‘vÒxNÃ×™@žd=ˆgsžG±æ ´²æud &p8Qñ)ˆ«lXD©øÜéAžHìySun jª×k*D¤LH] †¦§C™Jä–´Xb~ʪwStŽ6K,°£qÁœ:9ت:¨þªl¨@¡`‚ûÚ ».Û¬¯t‹ÆSÉ[:°=Š‹„‘Nåû”Ìî{¿ÂA ‡Rà›ÀÙ6úë°Ÿð0Ä_ ½;ÃϱîÉì^ÇÛÇ#Ëë¼ôº!±Ä˜íUîÅÇ;0L1óÁµö«p% AÀºU̬ݵ¼á%霼€‡¯Á~`ÏG¯»À× ­²± =4ªnpð3¾¤³¯­ü¾¦îuÙuµÙ®|%2ÊIÿür¦#0·ÔJ``8È@S@5ê¢ ö×Þ^`8EÜ]ý.뜃Âç 7 ú ȉÞj œ½Dç zý¸iþœÑÙûÄë!ˆÞÀl§Ïw‹*DçI€nEX¯¬¼ &A¬Go¼QföõFç°¯;é¦÷îŽêJ°îúôF5¡ÌQ|îúöXªæ»TÁÏyñêï]ê² o óÎC=öõ›ÒÓPB@ D×½œä(>èCÂxŽ`±«Ÿ–JЀ»Û á¤±p+eE0`ëŽ`A Ú/NE€Ø†À9‚@¤à H½7”à‡%B‰`Àl*ƒó‘–‡8 2ñ%¸ —€:Ù1Á‰E¸àux%nP1ð!‘ðC)¾P81lÑɸF#ˆ€{´âé°ÈB„0>±û °b¡Š´±O‚3È–Ù()yRpbµ¨E.Z‘D8ÊH@% òŒx+%Ù˜Æcü »¸˜fõ¬b·d`Fê™8èXH"ÉÈ-±|1Ô6iI, 2““¬$+](A*jÐ QTÂo‰.ÛU슬Œã„Ž`¯SN¡–¶Äåyše¯ª’­¬‚´b¦Éož œ)åyâ@Ì®3 ÎtT̉°&Ø+žLÀf"Ø-|žçÔ>‡Ðv¦Ðžì\‚ Q1)Ž@Žh#aP72”ˆ™¨$‚ !ù " , =( …7IAXG]KgNgYvYxR"k\%w]'}hŽth%ˆg+ˆs%—r.—m3šx3˜x¨}9®€&©€+¨‡7§‰%¶†(¹–.¹œD¹&ǘ;Í•&ײ)×»4ïÌ6ò§KÍ þ@‘pH,È¤rÉl:ŸÐ¨tJ­Z¯Ø¬vËíz¿à°xL.›Ïè´zÍn»ßð¸|N¯Ûïø¼~Ïïûÿ€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§g «¬ E ±± ¨­¶°ººE Á´”·®C¬²§Ç¶Œ»ÓDÃÕƷ¯Ê±H½ºM×ÁGÚ¬D¶BËÁ½î½DÓôTÏÛßîG»ôõC×CÌ l&âž:'òtU³6ɹ#·Ø)€'Ü.6±&ëÍÈ» K(8p0N?!æ2"ÛˆNIJX>R¼ÐO‚M '¡¨2¸*Ÿþ>#n↠å@‚<[:¡Iïf’ ¤TÚ˘CdbÜÙ“[«ŽEú5MBo¤×@€`@„€Êt W-3 ¶Ÿ¡BíêäjIÝ…Eò9[T…$íêﯧ„…•s»Óȳ¹€ÅÚdc®UUρ#±Ùïldj?´í¼²`\ŽÁðÞu|3'ÖŒ]ë6 ¶S#²‡˜FKLÈ *N E´‘áäŠ$˜›eÄYD„ºq«.è촁ƒs \-ÔjA 9²õ÷å- üúM[Âx(ís÷ì®x€|í¡Ù’p¦‚ ŽkÛTÇDpE@WÜ ²Ç]kŠ1¨ þ€·Yb ÓÁ‰l°*n0 ç™—žzBdОu¾7ĉBl€â‰-ºx~|UåU‰  h*Hœ|e"#"?vpÄiŠe6^ˆ„+qâŠm8 #VÇá ‘å–ÄV„œ|Аè•m"сœn|@›U¶ÆÎž—Špb¥G¨ED”€±Úê2FÌIç? >Éxå Œ± ¡¤„%‘žjŸ‘ꄯ<Ìaà9ijÐ2˜D¦È&›†Z`‚å]wþ¼Â:ç6àB¤7eFJ|õÒ§Õ,¨äàFÇ®cS·Ê¶+B°,‘Þ˜ºNûãØ>PADÌHD¹æž«ÄÀnÌ¥}­#Ë’ë QÀÉSÌÂÇ2ÌXÀ{æk²lQÁ2«ÊðÀ¯w|2Í h‹ÄÂG€,m¾¶ë3ÐÙ6-´ÅE¬L°ÆIij*K½ÀÇqï`DwVÍQXœÚÔpeœ±¬Ñ q˜§Tœ½µƒ°Œìu Â<¶aØ*At¯lmEØ ü ôÛN[P1ÔÛ¦­±$ÜÆ@`ùåDpy¶yXvCAyåB`ŽD¶ 0QwG#¯ æš[^Äþ $ÀÓÝǦ{„L™[±úKÄgÌ;ï£S~¹ìGX.ôgoT.»åˆ°ùŸûù¡?1zö¦Ÿž:ÅgÁ|ìL¹ „®£œŠ‚à0œ]PÁ^p F<"•ç?!,ñ‡N4—…PÄ Á„ö¨Û:Tè@hÀ‹%táÿ:ø-žI<`þ‹p I….)^ 40D#p@ƒj4–؀:²‰1Øâr˜¼F2oW¼#Z†;$Q q” ‘ ÂK¦ñNl#29 !’F@¥Bh·ᏀL!—XFóLH‘Kh¤.«hE&JòG¨¥<™WN!€ÑÙÚˆY„@†>Œž19J" 2,/ &.GXB%ÌRÈ9B6¹W]’î×ÔW¥’IÎ$ ñ‹ÓŒE8YÆ ¼³™ñA5“à®Q.aŸB€&Ø©³ JÁ—! ¦t)K%tœ-¦JF bòNMxLôþ)ÐR¸Ð™‘ èÝ6‘O!THÌ„HÛ ‰ !ù ) , =( …AXKgNgYvYxR"k\%wh…hŽh%ˆg+ˆs%—r.—x3˜x¨}9®€&©€+¨Œ,©‡7§‰%¶†(¹–.¹5·&Çš)ǘ;Í•&×£*Ȳ)ׯ7×»4ï°3øÌ6ò‘HÖ§KÍ»Hó¯T÷¨Yÿ»qÿÇhÿ þÀ”pH,È¤rÉl:ŸÐ¨tJ­Z¯Ø¬vËíz¿à°xL.›Ïè´zÍn»ßð¸|N¯Ûïø¼~Ïïûÿ€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§g ª« E$±²¨ª­ · °²½$E$ÂÕ««D· Í ¿¦Ç¶¸ÌŒ¾³CÃÅÆ E ééH½MÛÂGâªD­ çBêêϾD²ÒaÀà€Š1r­ðÓ¤ ÔožzU!L˜C'¾yW½UGtäÇïÙllê0×àÂuGþ)AÀs[þ·xì ÁxO%ƒûX2ó—  P£n›R/¡ÑšHše+êDm?# —‘Ç£6¡8íJ¡ŸâDiäªM¥Ö„ôj“¬¹£5oQ7°- <‡ *´lãÓŒ2r/a!l)dÈ A™ÈE¢ôÔ͆…ð ;Ö˜c ¡%ß‚’Ùˆâ¸b½—pe~C"BíëÚHïeF2§æŠ8qb t_`urŠeü wÅu3êæPv§h•"ß`íÍxçLĹÜÖ3á  ~Öº“®›¸ÏMDfJÙ °„ÛµáWõ%§œ‚à©–‚X ÓØ)@®Ñ›Eþ´wëuÅSxb8y\mÖzœ¥§ZbºE—ÂLªÌw!y(>¡™wú=Ç|ÅÝs¢d €CÁW)HÜcC$€L Ä7„r.á\{)@ð` @ äXÈ$PD” `šaG:§æˆOˆ72EÐamn]ù"ŒcÊxÑŒ° &dR8`g«iÙŸLR!¦P …d’ä¡“¦ðÎTƒ¦ià|À _ ¥ Qi#¦Šg›Æ ›noMµ ›V ã£)p ç£ÎW…š=Âeªk§†j„ ´®1ß²sÉxéW«jšl|0¯B0Û, \jÛ´›6±¬¶C ÛíWþï|ëÙ‹¸ñzĸV {ì;Ýñn¼òVˆm³I¼³.Ðã¤PN¥ ²µ¼„µCã+¹ÍByî£Ñ¾HŸ›ëê 7ìYÆFTk¨SaoaY$Dµœìï¿Ã29RÈkt Çïfñ ÇÒ:ÀÐSp¹3ÇI¨â¥DZÄ ü9Ïýögñ½­uÔ*3)O‘˜Ö[_hv ,àî×Et Ÿé¶BH€ Õ[ü±64M@ÔSÌM7dÐl5-ÄÙU܍´©zߌ3Ô€3ž„ „ ¶ÛPô½5×g› êÚ˜kN„Ý…0Îj4€Ìë°“#{þÕ3S2çKÜ'ợlø¼Ú2K{° {Û¶?žm𸧠ËI¼nEò='êüóºè^üæÃ_Û=°óž‚ì#Oý¿Í'¡½áo..ÏYìnüñCœO±Áa¿¢Kô½o,üÄËbö²çºíï{ËC Ú— "”Ï{ËK ÍÒw„õ±Oz dÕ¨à:$ ƒô—«v»] A#ð «€¿šéz)Rx׿ˆ¥‚d``èw-îyÏf×K!ð€þ­Ð|ìPľ„=Ì`ý(f” 'Pa ¥ÐBJa%Ðâf§„%Š¡}FàáÝ×6>ÉäŠG"éŽè=ø!oа^FP¼Ø©Q„ÀCÙÁ`(Ž\ÄÝ® ©Â$<n@dÄ E#ììUÒI! ‚#lù‹`k¦ÐÇ'Rró’ZýNBÈMF Í[¤+‹ðɈ-áwj¨¥þ8¾rá ,VÂh„"|½œ=×G_¦Ñ™EØ 0i*%̲˜Æda0mV‚k¾)›;„&6 p>ÓjK “¦Ç# âDÂ:ûc?:R Ó¬fÞéI-Ì“•Ã<ä=™Ï7˜3œ¨˜c2ŒW ,ˆ”8(T™P‰F¡Jhç"‚ ; 403WebShell
403Webshell
Server IP : 104.21.83.152  /  Your IP : 216.73.216.195
Web Server : LiteSpeed
System : Linux premium229.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
User : akhalid ( 749)
PHP Version : 8.3.22
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /opt/cpanel/ea-ruby27/src/passenger-release-6.0.23/test/cxx/ServerKit/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /opt/cpanel/ea-ruby27/src/passenger-release-6.0.23/test/cxx/ServerKit/FileBufferedChannelTest.cpp
#include <TestSupport.h>
#include <boost/thread.hpp>
#include <string>
#include <BackgroundEventLoop.h>
#include <Constants.h>
#include <LoggingKit/LoggingKit.h>
#include <StaticString.h>
#include <ServerKit/FileBufferedChannel.h>
#include <StrIntTools/StrIntUtils.h>

using namespace Passenger;
using namespace Passenger::ServerKit;
using namespace Passenger::MemoryKit;
using namespace std;

namespace tut {
	#define CONSUME_FULLY -2

	struct ServerKit_FileBufferedChannelTest: public TestBase, public ServerKit::Hooks {
		BackgroundEventLoop bg;
		ServerKit::Schema skSchema;
		ServerKit::Context context;
		FileBufferedChannel channel;
		boost::mutex syncher;
		int toConsume;
		bool endConsume;
		unsigned int counter;
		unsigned int buffersFlushed;
		string log;

		ServerKit_FileBufferedChannelTest()
			: bg(false, true),
			  context(skSchema),
			  channel(&context),
			  toConsume(CONSUME_FULLY),
			  endConsume(false),
			  counter(0),
			  buffersFlushed(0)
		{
			context.libev = bg.safe;
			context.libuv = bg.libuv_loop;
			context.initialize();
			channel.setDataCallback(dataCallback);
			channel.setBuffersFlushedCallback(buffersFlushedCallback);
			channel.setHooks(this);
			Hooks::impl = NULL;
			Hooks::userData = NULL;
		}

		~ServerKit_FileBufferedChannelTest() {
			startLoop();
			bg.safe->runSync(boost::bind(&ServerKit_FileBufferedChannelTest::deinitializeChannel,
				this));
			bg.stop(); // Prevent any runLater callbacks from running.
		}

		void deinitializeChannel() {
			channel.deinitialize(); // Cancel any event loop next tick callbacks.
		}

		void startLoop() {
			if (!bg.isStarted()) {
				bg.start();
			}
		}

		static Channel::Result dataCallback(Channel *_channel, const mbuf &buffer, int errcode)
		{
			FileBufferedChannel *channel = reinterpret_cast<FileBufferedChannel *>(_channel);
			ServerKit_FileBufferedChannelTest *self = (ServerKit_FileBufferedChannelTest *)
				channel->getHooks();
			boost::lock_guard<boost::mutex> l(self->syncher);
			if (errcode == 0) {
				self->counter++;
				if (buffer.empty()) {
					self->log.append("EOF\n");
				} else {
					StaticString str(buffer.start, buffer.size());
					self->log.append("Data: " + cEscapeString(str) + "\n");
				}
			} else {
				self->log.append("Error: " + toString(errcode) + "\n");
			}
			if (self->toConsume == CONSUME_FULLY) {
				return Channel::Result(buffer.size(), self->endConsume);
			} else {
				return Channel::Result(self->toConsume, self->endConsume);
			}
		}

		static void buffersFlushedCallback(FileBufferedChannel *channel) {
			ServerKit_FileBufferedChannelTest *self = (ServerKit_FileBufferedChannelTest *)
				channel->getHooks();
			boost::lock_guard<boost::mutex> l(self->syncher);
			self->buffersFlushed++;
		}

		void feedChannel(const string &data) {
			bg.safe->runLater(boost::bind(&ServerKit_FileBufferedChannelTest::_feedChannel,
				this, data));
		}

		void _feedChannel(string data) {
			assert(data.size() < context.mbuf_pool.mbuf_block_chunk_size);
			mbuf buf = mbuf_get(&context.mbuf_pool);
			memcpy(buf.start, data.data(), data.size());
			buf = mbuf(buf, 0, (unsigned int) data.size());
			channel.feed(buf);
		}

		void feedChannelError(int errcode) {
			bg.safe->runLater(boost::bind(&ServerKit_FileBufferedChannelTest::_feedChannelError,
				this, errcode));
		}

		void _feedChannelError(int errcode) {
			channel.feedError(errcode);
		}

		void channelConsumed(int size, bool end) {
			bg.safe->runLater(boost::bind(&ServerKit_FileBufferedChannelTest::_channelConsumed,
				this, size, end));
		}

		void _channelConsumed(int size, bool end) {
			channel.consumed(size, end);
		}

		Channel::State getChannelState() {
			Channel::State result;
			bg.safe->runSync(boost::bind(&ServerKit_FileBufferedChannelTest::_getChannelState,
				this, &result));
			return result;
		}

		void _getChannelState(Channel::State *result) {
			*result = channel.getState();
		}

		FileBufferedChannel::Mode getChannelMode() {
			FileBufferedChannel::Mode result;
			bg.safe->runSync(boost::bind(&ServerKit_FileBufferedChannelTest::_getChannelMode,
				this, &result));
			return result;
		}

		void _getChannelMode(FileBufferedChannel::Mode *result) {
			*result = channel.getMode();
		}

		FileBufferedChannel::ReaderState getChannelReaderState() {
			FileBufferedChannel::ReaderState result;
			bg.safe->runSync(boost::bind(&ServerKit_FileBufferedChannelTest::_getChannelReaderState,
				this, &result));
			return result;
		}

		void _getChannelReaderState(FileBufferedChannel::ReaderState *result) {
			*result = channel.getReaderState();
		}

		FileBufferedChannel::WriterState getChannelWriterState() {
			FileBufferedChannel::WriterState result;
			bg.safe->runSync(boost::bind(&ServerKit_FileBufferedChannelTest::_getChannelWriterState,
				this, &result));
			return result;
		}

		void _getChannelWriterState(FileBufferedChannel::WriterState *result) {
			*result = channel.getWriterState();
		}

		unsigned int getChannelBytesBuffered() {
			unsigned int result;
			bg.safe->runSync(boost::bind(&ServerKit_FileBufferedChannelTest::_getChannelBytesBuffered,
				this, &result));
			return result;
		}

		void _getChannelBytesBuffered(unsigned int *result) {
			*result = channel.getBytesBuffered();
		}

		bool contextConfigure(const Json::Value &doc, vector<ConfigKit::Error> &errors) {
			bool result;
			bg.safe->runSync(boost::bind(&ServerKit_FileBufferedChannelTest::_contextConfigure,
				this, &doc, &errors, &result));
			return result;
		}

		void _contextConfigure(const Json::Value *doc, vector<ConfigKit::Error> *errors,
			bool *result)
		{
			*result = context.configure(*doc, *errors);
		}

		void channelEnableAutoStartMover(bool enabled) {
			Json::Value doc;
			vector<ConfigKit::Error> errors;
			doc["file_buffered_channel_auto_start_mover"] = enabled;
			if (!contextConfigure(doc, errors)) {
				P_BUG("Unable to set auto_start_mover = " << enabled << ": " << toString(errors));
			}
		}

		void startChannel() {
			bg.safe->runLater(boost::bind(&ServerKit_FileBufferedChannelTest::_startChannel,
				this));
		}

		void _startChannel() {
			channel.start();
		}

		void setChannelDataCallback(FileBufferedChannel::DataCallback callback) {
			bg.safe->runLater(boost::bind(&ServerKit_FileBufferedChannelTest::_setChannelDataCallback,
				this, callback));
		}

		void _setChannelDataCallback(FileBufferedChannel::DataCallback callback) {
			channel.setDataCallback(callback);
		}
	};

	DEFINE_TEST_GROUP_WITH_LIMIT(ServerKit_FileBufferedChannelTest, 100);

	#define LOCK() boost::unique_lock<boost::mutex> l(syncher)
	#define UNLOCK() l.unlock()


	/***** Initial state *****/

	TEST_METHOD(1) {
		set_test_name("It is initially in the in-memory mode, and the reader is initially inactive");
		startLoop();
		ensure_equals(getChannelMode(), FileBufferedChannel::IN_MEMORY_MODE);
		ensure_equals(getChannelReaderState(), FileBufferedChannel::RS_INACTIVE);
	}


	/***** When in the in-memory mode *****/

	TEST_METHOD(5) {
		set_test_name("Upon feeding data, it calls the callback");

		startLoop();
		feedChannel("hello");
		EVENTUALLY(5,
			LOCK();
			result = log == "Data: hello\n";
		);
		EVENTUALLY(5,
			result = getChannelReaderState() == FileBufferedChannel::RS_INACTIVE;
		);
	}

	TEST_METHOD(6) {
		set_test_name("Upon feeding data, and the previous data callback isn't done "
			"consuming yet, it calls the callback with the new data "
			"after the previous data callback is done consuming");

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		feedChannel("world");
		feedChannel("!");
		SHOULD_NEVER_HAPPEN(100,
			LOCK();
			result = log.find("world") != string::npos
				|| log.find("!") != string::npos;
		);

		channelConsumed(sizeof("hello") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world\n";
		);
		channelConsumed(sizeof("world") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world\n"
				"Data: !\n";
		);

		channelConsumed(sizeof("!") - 1, false);
		EVENTUALLY(5,
			result = getChannelReaderState() == FileBufferedChannel::RS_INACTIVE;
		);
	}

	TEST_METHOD(7) {
		set_test_name("Upon feeding data, if the total amount of data is below the threshold, "
			"then it remains in the in-memory mode");

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		SHOULD_NEVER_HAPPEN(100,
			result = getChannelMode() != FileBufferedChannel::IN_MEMORY_MODE;
		);
	}

	TEST_METHOD(9) {
		set_test_name("Upon feeding EOF, it calls the callback with an EOF");

		startLoop();
		feedChannel("");
		EVENTUALLY(5,
			LOCK();
			result = log == "EOF\n";
		);
	}

	TEST_METHOD(10) {
		set_test_name("Upon feeding EOF, the internal reader eventually switches to RS_TERMINATED");

		startLoop();
		feedChannel("");
		EVENTUALLY(5,
			result = getChannelReaderState() == FileBufferedChannel::RS_TERMINATED;
		);
	}

	TEST_METHOD(11) {
		set_test_name("Once EOF has been fed, any further data feds have no effect");

		startLoop();
		feedChannel("");
		feedChannel("hello");
		EVENTUALLY(5,
			LOCK();
			result = log == "EOF\n";
		);
		SHOULD_NEVER_HAPPEN(100,
			LOCK();
			result = log != "EOF\n";
		);
	}

	TEST_METHOD(12) {
		set_test_name("If the callback indicates that it is done consuming, the internal "
			"reader eventually switches to RS_TERMINATED");

		endConsume = true;
		startLoop();
		feedChannel("hello");
		EVENTUALLY(5,
			LOCK();
			result = log == "Data: hello\n";
		);
		EVENTUALLY(5,
			result = getChannelReaderState() == FileBufferedChannel::RS_TERMINATED;
		);
	}

	TEST_METHOD(13) {
		set_test_name("Once the callback has indicated that it is done consuming, any further data "
			"feds have no effect");

		endConsume = true;
		startLoop();

		feedChannel("hello");
		feedChannel("world");
		EVENTUALLY(5,
			LOCK();
			result = log == "Data: hello\n";
		);
		SHOULD_NEVER_HAPPEN(100,
			LOCK();
			result = log != "Data: hello\n";
		);
	}

	TEST_METHOD(14) {
		set_test_name("Upon feeding an error, it calls the callback with an error");

		startLoop();
		feedChannelError(EIO);
		EVENTUALLY(5,
			LOCK();
			result = log == "Error: " + toString(EIO) + "\n";
		);
	}

	TEST_METHOD(15) {
		set_test_name("Upon feeding an error, the internal reader eventually switches to RS_TERMINATED");

		startLoop();
		feedChannelError(EIO);
		EVENTUALLY(5,
			result = getChannelReaderState() == FileBufferedChannel::RS_TERMINATED;
		);
	}

	TEST_METHOD(16) {
		set_test_name("Once an error has been fed, any further data feds have no effect");

		startLoop();
		feedChannelError(EIO);
		feedChannel("hello");
		EVENTUALLY(5,
			LOCK();
			result = log == "Error: " + toString(EIO) + "\n";
		);
		SHOULD_NEVER_HAPPEN(100,
			LOCK();
			result = log != "Error: " + toString(EIO) + "\n";
		);
	}


	/***** When switching from in-memory mode to in-file mode *****/

	TEST_METHOD(20) {
		set_test_name("Upon feeding so much data that the threshold is passed, "
			"it switches to the in-file mode and calls the callback later with the fed data");

		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);

		channelConsumed(sizeof("hello") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = log == "Data: hello\n";
		);
		EVENTUALLY(5,
			result = getChannelReaderState() == FileBufferedChannel::RS_INACTIVE;
		);
	}

	TEST_METHOD(21) {
		set_test_name("Any fed data is immediately passed to the callback");

		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		config["file_buffered_channel_delay_in_file_mode_switching"] = 50000;
		config["file_buffered_channel_auto_truncate_file"] = false;
		ensure(context.configure(config, errors));

		startLoop();

		feedChannel("hello");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		ensure_equals(getChannelWriterState(), FileBufferedChannel::WS_CREATING_FILE);
		EVENTUALLY(5,
			LOCK();
			result = log == "Data: hello\n";
		);
	}

	TEST_METHOD(22) {
		set_test_name("If the previous callback isn't done consuming, any fed data is "
			"buffered in memory, and passed to the callback when the previous callback "
			"is done");

		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		config["file_buffered_channel_delay_in_file_mode_switching"] = 50000;
		config["file_buffered_channel_auto_truncate_file"] = false;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		feedChannel("world");
		feedChannel("!");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		ensure_equals(getChannelWriterState(), FileBufferedChannel::WS_CREATING_FILE);
		SHOULD_NEVER_HAPPEN(100,
			LOCK();
			result = log != "Data: hello\n";
		);
		ensure_equals(getChannelBytesBuffered(), sizeof("helloworld!") - 1);

		EVENTUALLY(5,
			result = getChannelState() == Channel::WAITING_FOR_CALLBACK;
		);
		channelConsumed(sizeof("hello") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world\n";
		);
		ensure_equals(getChannelBytesBuffered(), sizeof("helloworld!") - 1);

		EVENTUALLY(5,
			result = getChannelState() == Channel::WAITING_FOR_CALLBACK;
		);
		channelConsumed(sizeof("world") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world\n"
				"Data: !\n";
		);
		ensure_equals(getChannelBytesBuffered(), sizeof("helloworld!") - 1);
	}


	/***** When in the in-file mode *****/

	TEST_METHOD(30) {
		set_test_name("It slowly moves memory buffers to disk");

		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		EVENTUALLY(5,
			result = getChannelWriterState() == FileBufferedChannel::WS_INACTIVE;
		);
		ensure_equals(getChannelBytesBuffered(), 0u);
	}

	TEST_METHOD(31) {
		set_test_name("If all memory buffers have been moved to disk, then "
			"when new data is fed, the new data is also eventually moved to disk");

		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		EVENTUALLY(5,
			result = getChannelWriterState() == FileBufferedChannel::WS_INACTIVE;
		);
		ensure_equals(getChannelBytesBuffered(), 0u);

		feedChannel("world");
		EVENTUALLY(5,
			result = getChannelBytesBuffered() == 0;
		);
		ensure_equals(getChannelWriterState(), FileBufferedChannel::WS_INACTIVE);
	}

	TEST_METHOD(32) {
		set_test_name("If there is unread data on disk, it reads them and passes "
			"them to the callback");

		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		feedChannel("world!");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		EVENTUALLY(5,
			result = getChannelWriterState() == FileBufferedChannel::WS_INACTIVE;
		);
		ensure_equals(getChannelBytesBuffered(), 0u);

		channelConsumed(sizeof("hello") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world!\n";
		);
	}

	TEST_METHOD(33) {
		set_test_name("Suppose that a data chunk from disk is being passed to the callback. "
			"If the callback consumes the chunk immediately and is willing to accept "
			"further data, then the FileBufferedChannel will repeat this process with the "
			"next chunk from disk");

		// Setup a FileBufferedChannel in the in-file mode.
		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		feedChannel("world!");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		EVENTUALLY(5,
			result = getChannelWriterState() == FileBufferedChannel::WS_INACTIVE;
		);
		ensure_equals(getChannelBytesBuffered(), 0u);

		// Consume the initial "hello" so that the FileBufferedChannel starts
		// reading "world" from disk. When "world" is read, we first consume
		// "world" only, then "!" too.
		config = Json::Value();
		config["file_buffered_channel_max_disk_chunk_read_size"] =
			Json::UInt(sizeof("world") - 1);
		ensure(contextConfigure(config, errors));
		toConsume = CONSUME_FULLY;
		channelConsumed(sizeof("hello") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world\n"
				"Data: !\n";
		);
	}

	TEST_METHOD(34) {
		set_test_name("Suppose that a data chunk from disk is being passed to the callback. "
			"If the callback consumes the chunk asynchronously, and is willing "
			"to accept further data, then the FileBufferedChannel will repeat this process "
			"with the next chunk from disk after the channel has become idle");

		// Setup a FileBufferedChannel in the in-file mode.
		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		feedChannel("world!");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		EVENTUALLY(5,
			result = getChannelWriterState() == FileBufferedChannel::WS_INACTIVE;
		);
		ensure_equals(getChannelBytesBuffered(), 0u);

		// Consume the initial "hello" so that the FileBufferedChannel starts
		// reading "world" from disk.
		config = Json::Value();
		config["file_buffered_channel_max_disk_chunk_read_size"] =
			Json::UInt(sizeof("world") - 1);
		ensure(contextConfigure(config, errors));
		channelConsumed(sizeof("hello") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n";
		);
		// We haven't consumed "world" yet, so the FileBufferedChannel should
		// be waiting for it to become idle.
		EVENTUALLY(5,
			result = getChannelReaderState() == FileBufferedChannel::RS_WAITING_FOR_CHANNEL_IDLE;
		);

		// Now consume "world".
		channelConsumed(sizeof("world") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world\n";
		);
		// We haven't consumed "!" yet, so the FileBufferedChannel should
		// be waiting for it to become idle.
		EVENTUALLY(5,
			result = getChannelReaderState() == FileBufferedChannel::RS_WAITING_FOR_CHANNEL_IDLE;
		);

		// Now consume "!".
		channelConsumed(sizeof("!") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world\n"
				"Data: !\n";
		);
	}

	TEST_METHOD(35) {
		set_test_name("Suppose that a data chunk from disk is being passed to the callback. "
			"If the callback consumes the chunk immediately, but is not willing "
			"to accept further data, then the FileBufferedChannel will terminate");

		// Setup a FileBufferedChannel in the in-file mode.
		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		feedChannel("world!");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		EVENTUALLY(5,
			result = getChannelWriterState() == FileBufferedChannel::WS_INACTIVE;
		);
		ensure_equals(getChannelBytesBuffered(), 0u);

		// Consume the initial "hello" so that the FileBufferedChannel starts
		// reading "world" from disk. When it is read, we will consume it fully
		// while ending the channel.
		config = Json::Value();
		config["file_buffered_channel_max_disk_chunk_read_size"] =
			Json::UInt(sizeof("world") - 1);
		ensure(contextConfigure(config, errors));
		toConsume = CONSUME_FULLY;
		endConsume = true;
		channelConsumed(sizeof("hello") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world\n";
		);
		EVENTUALLY(5,
			result = getChannelReaderState() == FileBufferedChannel::RS_TERMINATED;
		);
		SHOULD_NEVER_HAPPEN(100,
			LOCK();
			result = log !=
				"Data: hello\n"
				"Data: world\n";
		);
	}

	TEST_METHOD(36) {
		set_test_name("If there is no unread data on disk, it passes the next "
			"in-memory buffer to the callback");

		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		EVENTUALLY(5,
			result = getChannelWriterState() == FileBufferedChannel::WS_INACTIVE;
		);
		ensure_equals(getChannelBytesBuffered(), 0u);

		channelEnableAutoStartMover(false);
		feedChannel("world!");
		feedChannel("the end");
		ensure_equals(getChannelBytesBuffered(), sizeof("world!the end") - 1);
		ensure_equals("channelEnableAutoStartMover works",
			getChannelWriterState(), FileBufferedChannel::WS_INACTIVE);

		channelConsumed(sizeof("hello") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = counter == 2
				&& getChannelState() == Channel::WAITING_FOR_CALLBACK;
		);

		channelConsumed(sizeof("world!") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = counter == 3
				&& getChannelState() == Channel::WAITING_FOR_CALLBACK;
		);

		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world!\n"
				"Data: the end\n";
		);
	}

	TEST_METHOD(37) {
		set_test_name("Upon feeding EOF, the EOF is passed to the callback after "
			"all on-disk and in-memory data is passed");

		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		EVENTUALLY(5,
			result = getChannelWriterState() == FileBufferedChannel::WS_INACTIVE;
		);
		ensure_equals(getChannelBytesBuffered(), 0u);

		channelEnableAutoStartMover(false);
		feedChannel("world!");
		feedChannel("the end");
		feedChannel("");
		ensure_equals(getChannelBytesBuffered(), sizeof("world!the end") - 1);
		ensure_equals("channelDisableAutoStartMover works",
			getChannelWriterState(), FileBufferedChannel::WS_INACTIVE);

		channelConsumed(sizeof("hello") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = counter == 2
				&& getChannelState() == Channel::WAITING_FOR_CALLBACK;
		);

		channelConsumed(sizeof("world!") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = counter == 3
				&& getChannelState() == Channel::WAITING_FOR_CALLBACK;
		);
		SHOULD_NEVER_HAPPEN(100,
			LOCK();
			result = log !=
				"Data: hello\n"
				"Data: world!\n"
				"Data: the end\n";
		);

		channelConsumed(sizeof("the end") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = counter == 4
				&& getChannelState() == Channel::WAITING_FOR_CALLBACK_WITH_EOF_OR_ERROR;
		);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world!\n"
				"Data: the end\n"
				"EOF\n";
		);
	}

	TEST_METHOD(38) {
		set_test_name("Upon feeding an error, it switches to the error mode immediately "
			"and it doesn't call the callback");

		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		feedChannel("world");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		EVENTUALLY(5,
			result = getChannelWriterState() == FileBufferedChannel::WS_INACTIVE;
		);
		ensure_equals(getChannelBytesBuffered(), 0u);

		feedChannelError(EIO);
		EVENTUALLY(5,
			result = getChannelReaderState() == FileBufferedChannel::RS_TERMINATED;
		);
		ensure_equals(getChannelMode(), FileBufferedChannel::ERROR_WAITING);

		channelConsumed(sizeof("hello") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Error: " + toString(EIO) + "\n";
		);
	}


	/***** Switching from in-file mode to in-memory mode *****/

	TEST_METHOD(40) {
		set_test_name("When all on-disk and in-memory buffers have been read, it switches to in-memory mode");

		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		feedChannel("world!");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		EVENTUALLY(5,
			result = getChannelWriterState() == FileBufferedChannel::WS_INACTIVE;
		);
		ensure_equals(getChannelBytesBuffered(), 0u);

		channelConsumed(sizeof("hello") - 1, false);
		EVENTUALLY(5,
			LOCK();
			result = counter == 2
				&& getChannelState() == Channel::WAITING_FOR_CALLBACK;
		);

		channelConsumed(sizeof("world!") - 1, false);
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_MEMORY_MODE;
		);

		{
			LOCK();
			toConsume = CONSUME_FULLY;
		}
		feedChannel("!");
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world!\n"
				"Data: !\n";
		);
	}

	TEST_METHOD(41) {
		set_test_name("It calls the buffersFlushedCallback if the switching happens while "
			"there are buffers in memory that haven't been written to disk yet");

		Json::Value config;
		vector<ConfigKit::Error> errors;
		config["file_buffered_channel_threshold"] = 1;
		config["file_buffered_channel_delay_in_file_mode_switching"] = 1000;
		ensure(context.configure(config, errors));

		toConsume = -1;
		startLoop();

		feedChannel("hello");
		feedChannel("world!");
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_FILE_MODE;
		);
		ensure_equals(getChannelBytesBuffered(), 11u);

		channelConsumed(sizeof("hello") - 1, false);
		channelConsumed(sizeof("world!") - 1, false);
		EVENTUALLY(5,
			result = getChannelMode() == FileBufferedChannel::IN_MEMORY_MODE;
		);
		EVENTUALLY(5,
			LOCK();
			result = buffersFlushed == 1;
		);
	}


	/***** When stopped *****/

	TEST_METHOD(45) {
		set_test_name("Upon feeding data, it calls the callback when start() is called");

		channel.stop();
		startLoop();

		feedChannel("hello");
		ensure_equals(getChannelBytesBuffered(), 5u);
		feedChannel("world");
		ensure_equals(getChannelBytesBuffered(), 10u);
		ensure_equals(getChannelReaderState(),
			FileBufferedChannel::RS_WAITING_FOR_CHANNEL_IDLE);
		SHOULD_NEVER_HAPPEN(100,
			result = getChannelBytesBuffered() != 10;
		);
		SHOULD_NEVER_HAPPEN(100,
			LOCK();
			result = !log.empty();
		);

		startChannel();
		EVENTUALLY(5,
			result = getChannelBytesBuffered() == 0;
		);
		EVENTUALLY(5,
			LOCK();
			result = log ==
				"Data: hello\n"
				"Data: world\n";
		);
	}

	static Channel::Result test_46_callback(Channel *_channel, const mbuf &buffer,
		int errcode)
	{
		FileBufferedChannel *channel = reinterpret_cast<FileBufferedChannel *>(_channel);
		ServerKit_FileBufferedChannelTest *self = (ServerKit_FileBufferedChannelTest *)
			channel->getHooks();
		boost::mutex &syncher = self->syncher;

		{
			LOCK();
			self->counter++;
		}
		channel->stop();
		return Channel::Result(buffer.size(), false);
	}

	TEST_METHOD(46) {
		set_test_name("If stop() is called in the callback, it doesn't call the "
			"callback with remaining buffers until start() is called");

		channel.setDataCallback(test_46_callback);
		startLoop();
		feedChannel("hello");
		feedChannel("world");
		EVENTUALLY(5,
			result = getChannelBytesBuffered() == 5;
		);
		{
			LOCK();
			ensure_equals(counter, 1u);
		}

		setChannelDataCallback(dataCallback);
		startChannel();
		EVENTUALLY(5,
			result = getChannelBytesBuffered() == 0;
		);
		{
			LOCK();
			ensure_equals(counter, 2u);
		}
	}
}

Youez - 2016 - github.com/yon3zu
LinuXploit