From 30d72b041f3221b903ac11c0054dc221b0c0329b Mon Sep 17 00:00:00 2001
From: Monty Brandenberg <monty@lindenlab.com>
Date: Wed, 9 May 2012 10:23:02 -0400
Subject: Added correct libcurl initialization to the unit tests which makes
 Windows builds reliable. It's the right thing to do and introduced a scoped
 version for convenience in tests.

---
 indra/llcorehttp/tests/llcorehttp_test.cpp  |  5 +----
 indra/llcorehttp/tests/llcorehttp_test.h    | 25 +++++++++++++++++++++++++
 indra/llcorehttp/tests/test_httprequest.hpp | 14 ++++++++++----
 3 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/indra/llcorehttp/tests/llcorehttp_test.cpp b/indra/llcorehttp/tests/llcorehttp_test.cpp
index da811adb19..0ee767fdca 100644
--- a/indra/llcorehttp/tests/llcorehttp_test.cpp
+++ b/indra/llcorehttp/tests/llcorehttp_test.cpp
@@ -34,9 +34,6 @@
 // This works:
 #include "../test/lltut.h"
 
-#include <curl/curl.h>
-#include <openssl/crypto.h>
-
 // Pull in each of the test sets
 #include "test_httpstatus.hpp"
 #include "test_refcounted.hpp"
@@ -49,7 +46,7 @@
 unsigned long ssl_thread_id_callback(void);
 void ssl_locking_callback(int mode, int type, const char * file, int line);
 
-#if 0	// lltut provides main
+#if 0	// lltut provides main and runner
 
 namespace tut
 {
diff --git a/indra/llcorehttp/tests/llcorehttp_test.h b/indra/llcorehttp/tests/llcorehttp_test.h
index 941cb457d3..1550881a00 100644
--- a/indra/llcorehttp/tests/llcorehttp_test.h
+++ b/indra/llcorehttp/tests/llcorehttp_test.h
@@ -28,7 +28,32 @@
 #ifndef _LLCOREHTTP_TEST_H_
 #define	_LLCOREHTTP_TEST_H_
 
+#include "linden_common.h"		// Modifies curl interfaces
+
+#include <curl/curl.h>
+#include <openssl/crypto.h>
+
+// Initialization and cleanup for libcurl.  Mainly provides
+// a mutex callback for SSL and a thread ID hash for libcurl.
+// If you don't use these (or equivalent) and do use libcurl,
+// you'll see stalls and other anomalies when performing curl
+// operations.
 extern void init_curl();
 extern void term_curl();
 
+class ScopedCurlInit
+{
+public:
+	ScopedCurlInit()
+		{
+			init_curl();
+		}
+
+	~ScopedCurlInit()
+		{
+			term_curl();
+		}
+};
+	
+
 #endif	// _LLCOREHTTP_TEST_H_
diff --git a/indra/llcorehttp/tests/test_httprequest.hpp b/indra/llcorehttp/tests/test_httprequest.hpp
index a77a36c96e..a73d90957e 100644
--- a/indra/llcorehttp/tests/test_httprequest.hpp
+++ b/indra/llcorehttp/tests/test_httprequest.hpp
@@ -106,6 +106,8 @@ HttpRequestTestGroupType HttpRequestTestGroup("HttpRequest Tests");
 template <> template <>
 void HttpRequestTestObjectType::test<1>()
 {
+	ScopedCurlInit ready;
+	
 	set_test_name("HttpRequest construction");
 
 	// record the total amount of dynamically allocated memory
@@ -131,6 +133,8 @@ void HttpRequestTestObjectType::test<1>()
 template <> template <>
 void HttpRequestTestObjectType::test<2>()
 {
+	ScopedCurlInit ready;
+
 	set_test_name("HttpRequest and Null Op queued");
 
 	// record the total amount of dynamically allocated memory
@@ -168,6 +172,8 @@ void HttpRequestTestObjectType::test<2>()
 template <> template <>
 void HttpRequestTestObjectType::test<3>()
 {
+	ScopedCurlInit ready;
+
 	set_test_name("HttpRequest NoOp + Stop execution");
 
 	// Handler can be stack-allocated *if* there are no dangling
@@ -246,6 +252,8 @@ void HttpRequestTestObjectType::test<3>()
 template <> template <>
 void HttpRequestTestObjectType::test<4>()
 {
+	ScopedCurlInit ready;
+
 	set_test_name("2 HttpRequest instances, one thread");
 
 	// Handler can be stack-allocated *if* there are no dangling
@@ -335,8 +343,8 @@ void HttpRequestTestObjectType::test<4>()
 template <> template <>
 void HttpRequestTestObjectType::test<5>()
 {
-	init_curl();
-	
+	ScopedCurlInit ready;
+
 	set_test_name("HttpRequest GET + Stop execution");
 
 	// Handler can be stack-allocated *if* there are no dangling
@@ -419,8 +427,6 @@ void HttpRequestTestObjectType::test<5>()
 	ensure("Two handler calls on the way out", 2 == mHandlerCalls);
 	// printf("Old mem:  %d, New mem:  %d\n", mMemTotal, GetMemTotal());
 	ensure("Memory usage back to that at entry", mMemTotal == GetMemTotal());
-
-	term_curl();
 }
 
 }  // end namespace tut
-- 
cgit v1.2.3