PageRenderTime 37ms CodeModel.GetById 9ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/tests/commtest.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 145 lines | 90 code | 12 blank | 43 comment | 10 complexity | 669d1f7ace6eded59dd3924286eb0ab7 MD5 | raw file
  1/**
  2 * @file   commtest.h
  3 * @author Nat Goodspeed
  4 * @date   2009-01-09
  5 * @brief  
  6 * 
  7 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28
 29#if ! defined(LL_COMMTEST_H)
 30#define LL_COMMTEST_H
 31
 32#include "networkio.h"
 33#include "llevents.h"
 34#include "llsd.h"
 35#include "llhost.h"
 36#include "stringize.h"
 37#include <map>
 38#include <string>
 39#include <stdexcept>
 40#include <boost/lexical_cast.hpp>
 41
 42struct CommtestError: public std::runtime_error
 43{
 44    CommtestError(const std::string& what): std::runtime_error(what) {}
 45};
 46
 47static bool query_verbose()
 48{
 49    const char* cbose = getenv("INTEGRATION_TEST_VERBOSE");
 50    if (! cbose)
 51    {
 52        cbose = "1";
 53    }
 54    std::string strbose(cbose);
 55    return (! (strbose == "0" || strbose == "off" ||
 56               strbose == "false" || strbose == "quiet"));
 57}
 58
 59bool verbose()
 60{
 61    // This should only be initialized once.
 62    static bool vflag = query_verbose();
 63    return vflag;
 64}
 65
 66static int query_port(const std::string& var)
 67{
 68    const char* cport = getenv(var.c_str());
 69    if (! cport)
 70    {
 71        throw CommtestError(STRINGIZE("missing environment variable" << var));
 72    }
 73    // This will throw, too, if the value of PORT isn't numeric.
 74    int port(boost::lexical_cast<int>(cport));
 75    if (verbose())
 76    {
 77        std::cout << "getport('" << var << "') = " << port << std::endl;
 78    }
 79    return port;
 80}
 81
 82static int getport(const std::string& var)
 83{
 84    typedef std::map<std::string, int> portsmap;
 85    static portsmap ports;
 86    // We can do this with a single map lookup with map::insert(). Either it
 87    // returns an existing entry and 'false' (not newly inserted), or it
 88    // inserts the specified value and 'true'.
 89    std::pair<portsmap::iterator, bool> inserted(ports.insert(portsmap::value_type(var, 0)));
 90    if (inserted.second)
 91    {
 92        // We haven't yet seen this var. Remember its value.
 93        inserted.first->second = query_port(var);
 94    }
 95    // Return the (existing or new) iterator's value.
 96    return inserted.first->second;
 97}
 98
 99/**
100 * This struct is shared by a couple of standalone comm tests (ADD_COMM_BUILD_TEST).
101 */
102struct commtest_data
103{
104    NetworkIO& netio;
105    LLEventPumps& pumps;
106    LLEventStream replyPump, errorPump;
107    LLSD result;
108    bool success;
109    LLHost host;
110    std::string server;
111
112    commtest_data():
113        netio(NetworkIO::instance()),
114        pumps(LLEventPumps::instance()),
115        replyPump("reply"),
116        errorPump("error"),
117        success(false),
118        host("127.0.0.1", getport("PORT")),
119        server(STRINGIZE("http://" << host.getString() << "/"))
120    {
121        replyPump.listen("self", boost::bind(&commtest_data::outcome, this, _1, true));
122        errorPump.listen("self", boost::bind(&commtest_data::outcome, this, _1, false));
123    }
124
125    static int getport(const std::string& var)
126    {
127        // We have a couple consumers of commtest_data::getport(). But we've
128        // since moved it out to the global namespace. So this is just a
129        // facade.
130        return ::getport(var);
131    }
132
133    bool outcome(const LLSD& _result, bool _success)
134    {
135//      std::cout << "commtest_data::outcome(" << _result << ", " << _success << ")\n";
136        result = _result;
137        success = _success;
138        // Break the wait loop in NetworkIO::pump(), otherwise devs get
139        // irritated at making the big monolithic test executable take longer
140        pumps.obtain("done").post(success);
141        return false;
142    }
143};
144
145#endif /* ! defined(LL_COMMTEST_H) */