summaryrefslogtreecommitdiff
path: root/indra/newview/tests/llmediadataclient_test.cpp
blob: 495073cf670eefa99fe6088aff8aab371741b9da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
/** 
 * @file llmediadataclient_test.cpp
 * @brief LLMediaDatClient tests
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"
#include "../llviewerprecompiledheaders.h"
 
#include <iostream>
#include "../test/lltut.h"

#include "llsdserialize.h"
#include "llsdutil.h"
#include "llerrorcontrol.h"
#include "llhttpstatuscodes.h"

#include "../llmediadataclient.h"
#include "../llvovolume.h"

#include "../../llprimitive/llmediaentry.cpp"
#include "../../llprimitive/lltextureentry.cpp"
#include "../../llmessage/tests/llcurl_stub.cpp"

#if LL_WINDOWS
#pragma warning (push)
#pragma warning (disable : 4702) // boost::lexical_cast generates this warning
#endif
#include <boost/lexical_cast.hpp>
#if LL_WINDOWS
#pragma warning (pop)
#endif

#define VALID_OBJECT_ID   "3607d5c4-644b-4a8a-871a-8b78471af2a2"
#define VALID_OBJECT_ID_1 "11111111-1111-1111-1111-111111111111"
#define VALID_OBJECT_ID_2 "22222222-2222-2222-2222-222222222222"
#define VALID_OBJECT_ID_3 "33333333-3333-3333-3333-333333333333"
#define VALID_OBJECT_ID_4 "44444444-4444-4444-4444-444444444444"

#define FAKE_OBJECT_MEDIA_CAP_URL "foo_ObjectMedia"
#define FAKE_OBJECT_MEDIA_NAVIGATE_CAP_URL "foo_ObjectMediaNavigate"
#define FAKE_OBJECT_MEDIA_CAP_URL_503 "foo_ObjectMedia_503"
#define FAKE_OBJECT_MEDIA_NAVIGATE_CAP_URL_ERROR "foo_ObjectMediaNavigate_ERROR"

#define MEDIA_DATA "\
<array>														\
<string>foo</string>										\
<string>bar</string>										\
<string>baz</string>										\
</array>"

#define _DATA_URLS(ID,INTEREST,NEW,URL1,URL2) "					\
<llsd>											\
  <map>											\
    <key>uuid</key>								\
    <string>" ID "</string>						\
    <key>interest</key>											\
    <real>" INTEREST "</real>											\
    <key>cap_urls</key>											\
    <map>														\
      <key>ObjectMedia</key>									\
      <string>" URL1 "</string>			\
      <key>ObjectMediaNavigate</key>							\
      <string>" URL2 "</string>	\
    </map>														\
    <key>media_data</key>                                       \
	" MEDIA_DATA "												\
    <key>is_dead</key>											\
	<boolean>false</boolean>									\
	<key>is_new</key>											\
	<boolean>" NEW "</boolean>									\
  </map>														\
</llsd>"

#define _DATA(ID,INTEREST,NEW) _DATA_URLS(ID,INTEREST,NEW,FAKE_OBJECT_MEDIA_CAP_URL,FAKE_OBJECT_MEDIA_NAVIGATE_CAP_URL)

const char *DATA = _DATA(VALID_OBJECT_ID,"1.0","true");
	
#define STR(I) boost::lexical_cast<std::string>(I)

#define LOG_TEST(N) LL_DEBUGS("LLMediaDataClient") << "\n" <<			\
"================================================================================\n" << \
"==================================== TEST " #N " ===================================\n" << \
"================================================================================\n" << LL_ENDL;

LLSD *gPostRecords = NULL;
F64   gMinimumInterestLevel = (F64)0.0;

// stubs:
void LLHTTPClient::post(
		const std::string& url,
		const LLSD& body,
		LLHTTPClient::ResponderPtr responder,
		const LLSD& headers,
		const F32 timeout)
{
	LLSD record;
	record["url"] = url;
	record["body"] = body;
	record["headers"] = headers;
	record["timeout"] = timeout;
	gPostRecords->append(record);
	
	// Magic URL that triggers a 503:
	LLSD result;
	result[LLTextureEntry::OBJECT_ID_KEY] = body[LLTextureEntry::OBJECT_ID_KEY];
	if ( url == FAKE_OBJECT_MEDIA_CAP_URL_503 )
	{
		responder->error(HTTP_SERVICE_UNAVAILABLE, "fake reason");
		return;
	}
	else if (url == FAKE_OBJECT_MEDIA_NAVIGATE_CAP_URL_ERROR) 
	{
		LLSD error;
		error["code"] = LLObjectMediaNavigateClient::ERROR_PERMISSION_DENIED_CODE;
		result["error"] = error;
	}	
	responder->result(result);
}

const F32 HTTP_REQUEST_EXPIRY_SECS = 60.0f;

class LLMediaDataClientObjectTest : public LLMediaDataClientObject
{
public:
	LLMediaDataClientObjectTest(const char *data) 
		{
			std::istringstream d(data);
			LLSDSerialize::fromXML(mRep, d);
			mNumBounceBacks = 0;
            
           // std::cout << ll_pretty_print_sd(mRep) << std::endl;
           // std::cout << "ID: " << getID() << std::endl;
		}
	LLMediaDataClientObjectTest(const LLSD &rep) 
		: mRep(rep), mNumBounceBacks(0) {}
	~LLMediaDataClientObjectTest()
		{ LL_DEBUGS("LLMediaDataClient") << "~LLMediaDataClientObjectTest" << LL_ENDL; }
	
	virtual U8 getMediaDataCount() const 
		{ return mRep["media_data"].size(); }
	virtual LLSD getMediaDataLLSD(U8 index) const
		{ return mRep["media_data"][(LLSD::Integer)index]; }
	virtual LLUUID getID() const 
		{ return mRep["uuid"]; }
	virtual void mediaNavigateBounceBack(U8 index)
		{ mNumBounceBacks++; }	
	
	virtual bool hasMedia() const
		{ return mRep.has("media_data"); }
	
	virtual void updateObjectMediaData(LLSD const &media_data_array, const std::string &media_version)
		{ mRep["media_data"] = media_data_array; mRep["media_version"] = media_version; }
		
	virtual F64 getMediaInterest() const
		{ return (LLSD::Real)mRep["interest"]; }
	
	virtual bool isInterestingEnough() const
		{ return getMediaInterest() > gMinimumInterestLevel; }
	
	virtual std::string getCapabilityUrl(const std::string &name) const 
		{ return mRep["cap_urls"][name]; }

	virtual bool isDead() const
		{ return mRep["is_dead"]; }
	
	virtual U32 getMediaVersion() const
		{ return (LLSD::Integer)mRep["media_version"]; }
	
	virtual bool isNew() const
		{ return mRep["is_new"]; }
	
	void setMediaInterest(F64 val)
		{ mRep["interest"] = val; }

	int getNumBounceBacks() const
		{ return mNumBounceBacks; }
	
	void markDead()
		{ mRep["is_dead"] = true; }
	
	void markOld()
		{ mRep["is_new"] = false; }
	
private:
	LLSD mRep;
	int mNumBounceBacks;
};

// This special timer delay should ensure that the timer will fire on the very
// next pump, no matter what (though this does make an assumption about the
// implementation of LLEventTimer::updateClass()):
const F32 NO_PERIOD = -1000.0f;

static void pump_timers()
{
	LLEventTimer::updateClass();
}

namespace tut
{
    struct mediadataclient
    {
		mediadataclient() {
			gPostRecords = &mLLSD;
			gMinimumInterestLevel = (F64)0.0;
			
// 			LLError::setDefaultLevel(LLError::LEVEL_DEBUG);
// 			LLError::setClassLevel("LLMediaDataClient", LLError::LEVEL_DEBUG);
//			LLError::setTagLevel("MediaOnAPrim", LLError::LEVEL_DEBUG);
		}
		LLSD mLLSD;
    };
    
	typedef test_group<mediadataclient> mediadataclient_t;
	typedef mediadataclient_t::object mediadataclient_object_t;
	tut::mediadataclient_t tut_mediadataclient("mediadataclient");
    
    void ensure(const std::string &msg, int value, int expected)
    {
        std::string m = msg;
        m += " value: " + STR(value);
        m += ", expected: " + STR(expected);
        ensure(m, value == expected);
    }
    
    void ensure(const std::string &msg, const std::string & value, const std::string & expected)
    {
        std::string m = msg;
        m += " value: " + value;
        m += ", expected: " + expected;
        ensure(m, value == expected);
    }
    
    void ensure(const std::string &msg, const LLUUID & value, const LLUUID & expected)
    {
        std::string m = msg;
        m += " value: " + value.asString();
        m += ", expected: " + expected.asString();
        ensure(m, value == expected);
    }
    
    void ensure_llsd(const std::string &msg, const LLSD & value, const char *expected)
    {
        LLSD expected_llsd;
        std::istringstream e(expected);
        LLSDSerialize::fromXML(expected_llsd, e);
   
        std::string value_str = ll_pretty_print_sd(value);
        std::string expected_str = ll_pretty_print_sd(expected_llsd);
        std::string m = msg;
        m += " value: " + value_str;
        m += ", expected: " + expected_str;
        ensure(m, value_str == expected_str);
    }

	//////////////////////////////////////////////////////////////////////////////////////////
	
	template<> template<>
	void mediadataclient_object_t::test<1>()
	{
		//
		// Test fetchMedia()
		//
		LOG_TEST(1);
		
		LLMediaDataClientObject::ptr_t o = new LLMediaDataClientObjectTest(DATA);
		int num_refs_start = o->getNumRefs();
		{
			LLPointer<LLObjectMediaDataClient> mdc = new LLObjectMediaDataClient(NO_PERIOD,NO_PERIOD);
			mdc->fetchMedia(o);

			// Make sure no posts happened yet...
			ensure("post records", gPostRecords->size(), 0);

			::pump_timers();
		
			ensure("post records", gPostRecords->size(), 1);
			ensure("post url", (*gPostRecords)[0]["url"], FAKE_OBJECT_MEDIA_CAP_URL);
			ensure("post GET", (*gPostRecords)[0]["body"]["verb"], "GET");
			ensure("post object id", (*gPostRecords)[0]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), LLUUID(VALID_OBJECT_ID));
			ensure("queue empty", mdc->isEmpty());
		}
		
		// Make sure everyone's destroyed properly
		ensure("REF COUNT", o->getNumRefs(), num_refs_start);
    }

	//////////////////////////////////////////////////////////////////////////////////////////

	template<> template<>
	void mediadataclient_object_t::test<2>()
	{
		//
		// Test updateMedia()
		//
		LOG_TEST(2);

		LLMediaDataClientObject::ptr_t o = new LLMediaDataClientObjectTest(DATA);
		{
			// queue time w/ no delay ensures that ::pump_timers() will hit the tick()
			LLPointer<LLObjectMediaDataClient> mdc = new LLObjectMediaDataClient(NO_PERIOD,NO_PERIOD);  
			mdc->updateMedia(o);
			ensure("post records", gPostRecords->size(), 0);
			::pump_timers();
		
			ensure("post records", gPostRecords->size(), 1);
			ensure("post url", (*gPostRecords)[0]["url"], FAKE_OBJECT_MEDIA_CAP_URL);
			ensure("post UPDATE", (*gPostRecords)[0]["body"]["verb"], "UPDATE");
			ensure("post object id", (*gPostRecords)[0]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), LLUUID(VALID_OBJECT_ID));
			ensure_llsd("post data llsd", (*gPostRecords)[0]["body"][LLTextureEntry::OBJECT_MEDIA_DATA_KEY], 
						"<llsd>" MEDIA_DATA "</llsd>");
			ensure("queue empty", mdc->isEmpty());
		}

		ensure("REF COUNT", o->getNumRefs(), 1);
	}

	//////////////////////////////////////////////////////////////////////////////////////////

    template<> template<>
    void mediadataclient_object_t::test<3>()
    {
		//
		// Test navigate()
		//
		LOG_TEST(3);

		LLMediaDataClientObject::ptr_t o = new LLMediaDataClientObjectTest(DATA);
		{		
			LLPointer<LLObjectMediaNavigateClient> mdc = new LLObjectMediaNavigateClient(NO_PERIOD,NO_PERIOD);
			const char *TEST_URL = "http://example.com";
			mdc->navigate(o, 0, TEST_URL);
			ensure("post records", gPostRecords->size(), 0);
			::pump_timers();

			// ensure no bounce back
			ensure("bounce back", dynamic_cast<LLMediaDataClientObjectTest*>(static_cast<LLMediaDataClientObject*>(o))->getNumBounceBacks(), 0);
		
			ensure("post records", gPostRecords->size(), 1);
			ensure("post url", (*gPostRecords)[0]["url"], FAKE_OBJECT_MEDIA_NAVIGATE_CAP_URL);
			ensure("post object id", (*gPostRecords)[0]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), LLUUID(VALID_OBJECT_ID));
			ensure("post data", (*gPostRecords)[0]["body"][LLTextureEntry::TEXTURE_INDEX_KEY], 0);
			ensure("post data", (*gPostRecords)[0]["body"][LLMediaEntry::CURRENT_URL_KEY], TEST_URL);
			ensure("queue empty", mdc->isEmpty());
		}
		ensure("REF COUNT", o->getNumRefs(), 1);
    }
	
	//////////////////////////////////////////////////////////////////////////////////////////

    template<> template<>
    void mediadataclient_object_t::test<4>()
    {
		//
		// Test queue ordering
		//
		LOG_TEST(4);

		LLMediaDataClientObject::ptr_t o1 = new LLMediaDataClientObjectTest(
			_DATA(VALID_OBJECT_ID_1,"1.0","true"));
		LLMediaDataClientObject::ptr_t o2 = new LLMediaDataClientObjectTest(
			_DATA(VALID_OBJECT_ID_2,"3.0","true"));
		LLMediaDataClientObject::ptr_t o3 = new LLMediaDataClientObjectTest(
			_DATA(VALID_OBJECT_ID_3,"2.0","true"));
		{
			LLPointer<LLObjectMediaDataClient> mdc = new LLObjectMediaDataClient(NO_PERIOD,NO_PERIOD);  
			const char *ORDERED_OBJECT_IDS[] = { VALID_OBJECT_ID_2, VALID_OBJECT_ID_3, VALID_OBJECT_ID_1 };
			mdc->fetchMedia(o1);
			mdc->fetchMedia(o2);
			mdc->fetchMedia(o3);

			// Make sure no posts happened yet...
			ensure("post records", gPostRecords->size(), 0);

			// tick 3 times...
			::pump_timers();
			ensure("post records", gPostRecords->size(), 1);
			::pump_timers();
			ensure("post records", gPostRecords->size(), 2);
			::pump_timers();
			ensure("post records", gPostRecords->size(), 3);
		
			for( int i=0; i < 3; i++ )
			{
				ensure("[" + STR(i) + "] post url", (*gPostRecords)[i]["url"], FAKE_OBJECT_MEDIA_CAP_URL);
				ensure("[" + STR(i) + "] post GET", (*gPostRecords)[i]["body"]["verb"], "GET");
				ensure("[" + STR(i) + "] post object id", (*gPostRecords)[i]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), 
					   LLUUID(ORDERED_OBJECT_IDS[i]));
			}

			ensure("queue empty", mdc->isEmpty());
		}
		ensure("refcount of o1", o1->getNumRefs(), 1);
		ensure("refcount of o2", o2->getNumRefs(), 1);
		ensure("refcount of o3", o3->getNumRefs(), 1);
    }

	//////////////////////////////////////////////////////////////////////////////////////////

	template<> template<>
	void mediadataclient_object_t::test<5>()
	{
		//
		// Test fetchMedia() getting a 503 error
		//
		LOG_TEST(5);
		
		LLMediaDataClientObject::ptr_t o = new LLMediaDataClientObjectTest(
			_DATA_URLS(VALID_OBJECT_ID,
					   "1.0","true",
					   FAKE_OBJECT_MEDIA_CAP_URL_503,
					   FAKE_OBJECT_MEDIA_NAVIGATE_CAP_URL));
		int num_refs_start = o->getNumRefs();
		{
			const int NUM_RETRIES = 5;
			LLPointer<LLObjectMediaDataClient> mdc = new LLObjectMediaDataClient(NO_PERIOD,NO_PERIOD,NUM_RETRIES);

			// This should generate a retry
			mdc->fetchMedia(o);

			// Make sure no posts happened yet...
			ensure("post records before", gPostRecords->size(), 0);

			// Once, causes retry
			// Second, fires retry timer
			// Third, fires queue timer again
			for (int i=0; i<NUM_RETRIES; ++i)
			{
				::pump_timers();  // Should pump (fire) the queue timer, causing a retry timer to be scheduled
				// XXX This ensure is not guaranteed, because scheduling a timer might actually get it pumped in the same loop
				//ensure("post records " + STR(i), gPostRecords->size(), i+1);
				::pump_timers();  // Should pump (fire) the retry timer, scheduling the queue timer
			}

			// Do some extra pumps to make sure no other timer work occurs.
			::pump_timers();
			::pump_timers();
			::pump_timers();
			
			// Make sure there were 2 posts
			ensure("post records after", gPostRecords->size(), NUM_RETRIES);
			for (int i=0; i<NUM_RETRIES; ++i)
			{
				ensure("[" + STR(i) + "] post url", (*gPostRecords)[i]["url"], FAKE_OBJECT_MEDIA_CAP_URL_503);
				ensure("[" + STR(i) + "] post GET", (*gPostRecords)[i]["body"]["verb"], "GET");
				ensure("[" + STR(i) + "] post object id", (*gPostRecords)[i]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), LLUUID(VALID_OBJECT_ID));
			}
			ensure("queue empty", mdc->isEmpty());
		}
		
		// Make sure everyone's destroyed properly
		ensure("REF COUNT", o->getNumRefs(), num_refs_start);
    }

    template<> template<>
    void mediadataclient_object_t::test<6>()
    {
		//
		// Test navigate() with a bounce back
		//
		LOG_TEST(6);

		LLMediaDataClientObject::ptr_t o = new LLMediaDataClientObjectTest(
			_DATA_URLS(VALID_OBJECT_ID,
					   "1.0","true",
					   FAKE_OBJECT_MEDIA_CAP_URL,
					   FAKE_OBJECT_MEDIA_NAVIGATE_CAP_URL_ERROR));
		{		
			LLPointer<LLObjectMediaNavigateClient> mdc = new LLObjectMediaNavigateClient(NO_PERIOD,NO_PERIOD);
			const char *TEST_URL = "http://example.com";
			mdc->navigate(o, 0, TEST_URL);
			ensure("post records", gPostRecords->size(), 0);
			::pump_timers();

			// ensure bounce back
			ensure("bounce back", 
				   dynamic_cast<LLMediaDataClientObjectTest*>(static_cast<LLMediaDataClientObject*>(o))->getNumBounceBacks(),
				   1);
			
			ensure("post records", gPostRecords->size(), 1);
			ensure("post url", (*gPostRecords)[0]["url"], FAKE_OBJECT_MEDIA_NAVIGATE_CAP_URL_ERROR);
			ensure("post object id", (*gPostRecords)[0]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), LLUUID(VALID_OBJECT_ID));
			ensure("post data", (*gPostRecords)[0]["body"][LLTextureEntry::TEXTURE_INDEX_KEY], 0);
			ensure("post data", (*gPostRecords)[0]["body"][LLMediaEntry::CURRENT_URL_KEY], TEST_URL);
			ensure("queue empty", mdc->isEmpty());
		}
		ensure("REF COUNT", o->getNumRefs(), 1);
    }

	template<> template<>
    void mediadataclient_object_t::test<7>()
    {
		// Test LLMediaDataClient::isInQueue()
		LOG_TEST(7);
		
		LLMediaDataClientObject::ptr_t o1 = new LLMediaDataClientObjectTest(
			_DATA(VALID_OBJECT_ID_1,"3.0","true"));
		LLMediaDataClientObject::ptr_t o2 = new LLMediaDataClientObjectTest(
			_DATA(VALID_OBJECT_ID_2,"1.0","true"));
		int num_refs_start = o1->getNumRefs();
		{
			LLPointer<LLObjectMediaDataClient> mdc = new LLObjectMediaDataClient(NO_PERIOD,NO_PERIOD);
			
			ensure("not in queue yet 1", ! mdc->isInQueue(o1));
			ensure("not in queue yet 2", ! mdc->isInQueue(o2));
			
			mdc->fetchMedia(o1);
			
			ensure("is in queue", mdc->isInQueue(o1));
			ensure("is not in queue", ! mdc->isInQueue(o2));
			
			::pump_timers();
			
			ensure("not in queue anymore", ! mdc->isInQueue(o1));
			ensure("still is not in queue", ! mdc->isInQueue(o2));
			
			ensure("queue empty", mdc->isEmpty());
		}
		
		// Make sure everyone's destroyed properly
		ensure("REF COUNT", o1->getNumRefs(), num_refs_start);
		
	}

	template<> template<>
    void mediadataclient_object_t::test<8>()
    {
		// Test queue handling of objects that are marked dead.
		LOG_TEST(8);
		
		LLMediaDataClientObject::ptr_t o1 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_1,"4.0","true"));
		LLMediaDataClientObject::ptr_t o2 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_2,"3.0","true"));
		LLMediaDataClientObject::ptr_t o3 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_3,"2.0","true"));
		LLMediaDataClientObject::ptr_t o4 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_4,"1.0","true"));
		{
			LLPointer<LLObjectMediaDataClient> mdc = new LLObjectMediaDataClient(NO_PERIOD,NO_PERIOD);
			
			// queue up all 4 objects
			mdc->fetchMedia(o1);
			mdc->fetchMedia(o2);
			mdc->fetchMedia(o3);
			mdc->fetchMedia(o4);
			
			// and mark the second and fourth ones dead.
			dynamic_cast<LLMediaDataClientObjectTest*>(static_cast<LLMediaDataClientObject*>(o2))->markDead();
			dynamic_cast<LLMediaDataClientObjectTest*>(static_cast<LLMediaDataClientObject*>(o4))->markDead();

			ensure("is in queue 1", mdc->isInQueue(o1));
			ensure("is in queue 2", mdc->isInQueue(o2));
			ensure("is in queue 3", mdc->isInQueue(o3));
			ensure("is in queue 4", mdc->isInQueue(o4));
			ensure("post records", gPostRecords->size(), 0);
			
			::pump_timers();
			
			// The first tick should remove the first one 
			ensure("is not in queue 1", !mdc->isInQueue(o1));
			ensure("is in queue 2", mdc->isInQueue(o2));
			ensure("is in queue 3", mdc->isInQueue(o3));
			ensure("is in queue 4", mdc->isInQueue(o4));
			ensure("post records", gPostRecords->size(), 1);
			
			::pump_timers();
			
			// The second tick should skip the second and remove the third
			ensure("is not in queue 2", !mdc->isInQueue(o2));
			ensure("is not in queue 3", !mdc->isInQueue(o3));
			ensure("is in queue 4", mdc->isInQueue(o4));
			ensure("post records", gPostRecords->size(), 2);

			::pump_timers();

			// The third tick should skip the fourth one and empty the queue.
			ensure("is not in queue 4", !mdc->isInQueue(o4));
			ensure("post records", gPostRecords->size(), 2);

			ensure("queue empty", mdc->isEmpty());
		}
		ensure("refcount of o1", o1->getNumRefs(), 1);
		ensure("refcount of o2", o2->getNumRefs(), 1);
		ensure("refcount of o3", o3->getNumRefs(), 1);
		ensure("refcount of o4", o4->getNumRefs(), 1);

	}
	
	//////////////////////////////////////////////////////////////////////////////////////////

    template<> template<>
    void mediadataclient_object_t::test<9>()
    {
		//
		// Test queue re-ordering
		//
		LOG_TEST(9);
		
		LLMediaDataClientObject::ptr_t o1 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_1,"40.0","true"));
		LLMediaDataClientObject::ptr_t o2 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_2,"30.0","true"));
		LLMediaDataClientObject::ptr_t o3 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_3,"20.0","true"));
		LLMediaDataClientObjectTest *object4 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_4,"10.0","true"));
		LLMediaDataClientObject::ptr_t o4 = object4;
		{
			LLPointer<LLObjectMediaDataClient> mdc = new LLObjectMediaDataClient(NO_PERIOD,NO_PERIOD);
			
			// queue up all 4 objects.  They should now be in the queue in
			// order 1 through 4, with 4 being at the front of the queue
			mdc->fetchMedia(o1);
			mdc->fetchMedia(o2);
			mdc->fetchMedia(o3);
			mdc->fetchMedia(o4);
			
			int tick_num = 0;
			
			ensure(STR(tick_num) + ". is in queue 1", mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 2", mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is in queue 3", mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is in queue 4", mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 0);
			
			::pump_timers();
			++tick_num;
			
			// The first tick should remove the first one 
			ensure(STR(tick_num) + ". is not in queue 1", !mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 2", mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is in queue 3", mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is in queue 4", mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 1);
			
			// Now, pretend that object 4 moved relative to the avatar such
			// that it is now closest
			object4->setMediaInterest(50.0);
			
			::pump_timers();
			++tick_num;
			
			// The second tick should still pick off item 2, but then re-sort
			// have picked off object 4
			ensure(STR(tick_num) + ". is in queue 2", mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is in queue 3", mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is not in queue 4", !mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 2);

			::pump_timers();
			++tick_num;
			
			// The third tick should pick off object 2
			ensure(STR(tick_num) + ". is not in queue 2", !mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is in queue 3", mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 3);

			// The fourth tick should pick off object 3
			::pump_timers();
			++tick_num;

			ensure(STR(tick_num) + ". is not in queue 3", !mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 4);

			ensure("queue empty", mdc->isEmpty());
		}
		ensure("refcount of o1", o1->getNumRefs(), 1);
		ensure("refcount of o2", o2->getNumRefs(), 1);
		ensure("refcount of o3", o3->getNumRefs(), 1);
		ensure("refcount of o4", o4->getNumRefs(), 1);
    }
	
	
	template<> template<>
    void mediadataclient_object_t::test<10>()
    {
		//
		// Test using the "round-robin" queue
		//
		LOG_TEST(10);
		
		LLMediaDataClientObject::ptr_t o1 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_1,"1.0","true"));
		LLMediaDataClientObject::ptr_t o2 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_2,"2.0","true"));
		LLMediaDataClientObject::ptr_t o3 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_3,"3.0","false"));
		LLMediaDataClientObject::ptr_t o4 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_4,"4.0","false"));
		{
			LLPointer<LLObjectMediaDataClient> mdc = new LLObjectMediaDataClient(NO_PERIOD,NO_PERIOD);
			
			// queue up all 4 objects.  The first two should be in the sorted
			// queue [2 1], the second in the round-robin queue.  The queues
			// are serviced interleaved, so we should expect:
			// 2, 4, 1, 3
			mdc->fetchMedia(o1);
			mdc->fetchMedia(o2);
			mdc->fetchMedia(o3);
			mdc->fetchMedia(o4);
			
			int tick_num = 0;
			
			// 0
			ensure(STR(tick_num) + ". is in queue 1", mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 2", mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is in queue 3", mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is in queue 4", mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 0);
			
			::pump_timers();
			++tick_num;
			
			// 1 The first tick should remove object 2
			ensure(STR(tick_num) + ". is not in queue 2", !mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is in queue 1", mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 3", mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is in queue 4", mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 1);
			ensure(STR(tick_num) + ". post object id", (*gPostRecords)[0]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), LLUUID(VALID_OBJECT_ID_2));
			
			::pump_timers();
			++tick_num;
			
			// 2 The second tick should send object 4, but it will still be
			// "in the queue"
			ensure(STR(tick_num) + ". is not in queue 2", !mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is in queue 1", mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 3", mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is in queue 4", mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 2);
			ensure(STR(tick_num) + ". post object id", (*gPostRecords)[1]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), LLUUID(VALID_OBJECT_ID_4));
			
			::pump_timers();
			++tick_num;
			
			// 3 The third tick should remove object 1
			ensure(STR(tick_num) + ". is not in queue 2", !mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is not in queue 1", !mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 3", mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is in queue 4", mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 3);
			ensure(STR(tick_num) + ". post object id", (*gPostRecords)[2]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), LLUUID(VALID_OBJECT_ID_1));
			
			::pump_timers();
			++tick_num;
			
			// 4 The fourth tick should send object 3, but it will still be
			// "in the queue"
			ensure(STR(tick_num) + ". is not in queue 2", !mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is not in queue 1", !mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 3", mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is in queue 4", mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 4);
			ensure(STR(tick_num) + ". post object id", (*gPostRecords)[3]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), LLUUID(VALID_OBJECT_ID_3));
			
			::pump_timers();
			++tick_num;
						
			// 5 The fifth tick should now identify objects 3 and 4 as no longer
			// needing "updating", and remove them from the queue
			ensure(STR(tick_num) + ". is not in queue 2", !mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is not in queue 1", !mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is not in queue 3", !mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is not in queue 4", !mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 4);
			
			::pump_timers();
			
			// Whew....better be empty
			ensure("queue empty", mdc->isEmpty());
		}
		ensure("refcount of o1", o1->getNumRefs(), 1);
		ensure("refcount of o2", o2->getNumRefs(), 1);
		ensure("refcount of o3", o3->getNumRefs(), 1);
		ensure("refcount of o4", o4->getNumRefs(), 1);		
	}
	
	
	template<> template<>
	void mediadataclient_object_t::test<11>()
	{
		//
		// Test LLMediaDataClient's destructor
		//
		LOG_TEST(11);
		
		LLMediaDataClientObject::ptr_t o = new LLMediaDataClientObjectTest(DATA);
		int num_refs_start = o->getNumRefs();
		{
			LLPointer<LLObjectMediaDataClient> mdc = new LLObjectMediaDataClient(NO_PERIOD,NO_PERIOD);
			mdc->fetchMedia(o);
			// must tick enough times to clear refcount of mdc
			::pump_timers();
		}		
		// Make sure everyone's destroyed properly
		ensure("REF COUNT", o->getNumRefs(), num_refs_start);
	}
	
	template<> template<>
    void mediadataclient_object_t::test<12>()
    {
		//
		// Test the "not interesting enough" call
		//
		LOG_TEST(12);
		
		LLMediaDataClientObjectTest *object1 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_1,"1.0","true"));
		LLMediaDataClientObject::ptr_t o1 = object1;
		LLMediaDataClientObject::ptr_t o2 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_2,"2.0","true"));
		LLMediaDataClientObject::ptr_t o3 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_3,"3.0","true"));
		LLMediaDataClientObject::ptr_t o4 = new LLMediaDataClientObjectTest(_DATA(VALID_OBJECT_ID_4,"4.0","true"));
		{
			LLPointer<LLObjectMediaDataClient> mdc = new LLObjectMediaDataClient(NO_PERIOD,NO_PERIOD);
			
			// queue up all 4 objects.  The first two are "interesting enough".
			// Firing the timer 4 times should therefore leave them.
			// Note that they should be sorted 4,3,2,1
			// Then, we'll make one "interesting enough", fire the timer a few 
			// times, and make sure only it gets pulled off the queue
			gMinimumInterestLevel = 2.5;
			mdc->fetchMedia(o1);
			mdc->fetchMedia(o2);
			mdc->fetchMedia(o3);
			mdc->fetchMedia(o4);
			
			int tick_num = 0;
			
			// 0
			ensure(STR(tick_num) + ". is in queue 1", mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 2", mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is in queue 3", mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is in queue 4", mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 0);
			
			::pump_timers();
			++tick_num;
			
			// 1 The first tick should remove object 4
			ensure(STR(tick_num) + ". is in queue 1", mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 2", mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is in queue 3", mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is not in queue 4", !mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 1);
			ensure(STR(tick_num) + ". post object id", (*gPostRecords)[0]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), LLUUID(VALID_OBJECT_ID_4));
			
			::pump_timers();
			++tick_num;
			
			// 2 The second tick should send object 3
			ensure(STR(tick_num) + ". is in queue 1", mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 2", mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is not in queue 3", !mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is not in queue 4", !mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 2);
			ensure(STR(tick_num) + ". post object id", (*gPostRecords)[1]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), LLUUID(VALID_OBJECT_ID_3));
			
			::pump_timers();
			++tick_num;
			
			// 3 The third tick should not pull off anything
			ensure(STR(tick_num) + ". is in queue 1", mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 2", mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is not in queue 3", !mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is not in queue 4", !mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 2);

			::pump_timers();
			++tick_num;
			
			// 4 The fourth tick (for good measure) should not pull off anything
			ensure(STR(tick_num) + ". is in queue 1", mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 2", mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is not in queue 3", !mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is not in queue 4", !mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 2);
			
			// Okay, now futz with object 1's interest, such that it is now 
			// "interesting enough"
			object1->setMediaInterest((F64)5.0);
			
			// This should sort so that the queue is now [1 2] 
			::pump_timers();
			++tick_num;
			
			// 5 The fifth tick should now identify objects 3 and 4 as no longer
			// needing "updating", and remove them from the queue
			ensure(STR(tick_num) + ". is not in queue 1", !mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 2", mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is not in queue 3", !mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is not in queue 4", !mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 3);
			ensure(STR(tick_num) + ". post object id", (*gPostRecords)[2]["body"][LLTextureEntry::OBJECT_ID_KEY].asUUID(), LLUUID(VALID_OBJECT_ID_1));
			
			::pump_timers();
			++tick_num;
			
			// 6 The sixth tick should not pull off anything
			ensure(STR(tick_num) + ". is not in queue 1", !mdc->isInQueue(o1));
			ensure(STR(tick_num) + ". is in queue 2", mdc->isInQueue(o2));
			ensure(STR(tick_num) + ". is not in queue 3", !mdc->isInQueue(o3));
			ensure(STR(tick_num) + ". is not in queue 4", !mdc->isInQueue(o4));
			ensure(STR(tick_num) + ". post records", gPostRecords->size(), 3);
			
			::pump_timers();
			++tick_num;
		
			// Whew....better NOT be empty ... o2 should still be there
			ensure("queue not empty", !mdc->isEmpty());
			
			// But, we need to clear the queue, or else we won't destroy MDC...
			// this is a strange interplay between the queue timer and the MDC
			ensure("o2 couldn't be removed from queue", mdc->removeFromQueue(o2));
			// tick
			::pump_timers();
		}
		ensure("refcount of o1", o1->getNumRefs(), 1);
		ensure("refcount of o2", o2->getNumRefs(), 1);
		ensure("refcount of o3", o3->getNumRefs(), 1);
		ensure("refcount of o4", o4->getNumRefs(), 1);		
	}
}