| OLD | NEW |
| (Empty) |
| 1 // Copyright 2006-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 #include "omaha/third_party/gtest/include/gtest/gtest.h" | |
| 16 #include "const-win32.h" | |
| 17 #include "aggregator-win32_unittest.h" | |
| 18 #include "persistent_iterator-win32.h" | |
| 19 #include <atlbase.h> | |
| 20 #include <atlcom.h> | |
| 21 #include <iterator> | |
| 22 #include <map> | |
| 23 | |
| 24 using namespace stats_report; | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 class PersistentMetricsIteratorWin32Test: public MetricsAggregatorWin32Test { | |
| 29 public: | |
| 30 bool WriteStats() { | |
| 31 // put some persistent metrics into the registry | |
| 32 MetricsAggregatorWin32 agg(coll_, kAppName); | |
| 33 AddStats(); | |
| 34 bool ret = agg.AggregateMetrics(); | |
| 35 | |
| 36 // Reset the stats, we should now have the same stats | |
| 37 // in our collection as in registry. | |
| 38 AddStats(); | |
| 39 | |
| 40 return ret; | |
| 41 } | |
| 42 | |
| 43 typedef std::map<std::string, MetricBase*> MetricsMap; | |
| 44 void IndexMetrics(MetricsMap *metrics) { | |
| 45 // build a map over the metrics in our collection | |
| 46 MetricIterator it(coll_), end; | |
| 47 | |
| 48 for (; it != end; ++it) { | |
| 49 metrics->insert(std::make_pair(std::string(it->name()), *it)); | |
| 50 } | |
| 51 } | |
| 52 }; | |
| 53 | |
| 54 // compare two metrics instances for equality | |
| 55 bool equals(MetricBase *a, MetricBase *b) { | |
| 56 if (!a || !b) | |
| 57 return false; | |
| 58 | |
| 59 if (a->type() != b->type() || 0 != strcmp(a->name(), b->name())) | |
| 60 return false; | |
| 61 | |
| 62 switch (a->type()) { | |
| 63 case kCountType: | |
| 64 return a->AsCount().value() == b->AsCount().value(); | |
| 65 break; | |
| 66 case kTimingType: { | |
| 67 TimingMetric &at = a->AsTiming(); | |
| 68 TimingMetric &bt = b->AsTiming(); | |
| 69 | |
| 70 return at.count() == bt.count() && | |
| 71 at.sum() == bt.sum() && | |
| 72 at.minimum() == bt.minimum() && | |
| 73 at.maximum() == bt.maximum(); | |
| 74 } | |
| 75 break; | |
| 76 case kIntegerType: | |
| 77 return a->AsInteger().value() == b->AsInteger().value(); | |
| 78 break; | |
| 79 case kBoolType: | |
| 80 return a->AsBool().value() == b->AsBool().value(); | |
| 81 break; | |
| 82 | |
| 83 case kInvalidType: | |
| 84 default: | |
| 85 LOG(FATAL) << "Impossible metric type"; | |
| 86 } | |
| 87 | |
| 88 return false; | |
| 89 } | |
| 90 | |
| 91 } // namespace | |
| 92 | |
| 93 TEST_F(PersistentMetricsIteratorWin32Test, Basic) { | |
| 94 EXPECT_TRUE(WriteStats()); | |
| 95 PersistentMetricsIteratorWin32 a, b, c(kAppName); | |
| 96 | |
| 97 EXPECT_TRUE(a == b); | |
| 98 EXPECT_TRUE(b == a); | |
| 99 | |
| 100 EXPECT_FALSE(a == c); | |
| 101 EXPECT_FALSE(b == c); | |
| 102 EXPECT_FALSE(c == a); | |
| 103 EXPECT_FALSE(c == b); | |
| 104 | |
| 105 ++a; | |
| 106 EXPECT_TRUE(a == b); | |
| 107 EXPECT_TRUE(b == a); | |
| 108 } | |
| 109 | |
| 110 // Test to see whether we can reliably roundtrip metrics through | |
| 111 // the registry without molestation | |
| 112 TEST_F(PersistentMetricsIteratorWin32Test, UnmolestedValues) { | |
| 113 EXPECT_TRUE(WriteStats()); | |
| 114 | |
| 115 MetricsMap metrics; | |
| 116 IndexMetrics(&metrics); | |
| 117 | |
| 118 PersistentMetricsIteratorWin32 it(kAppName), end; | |
| 119 int count = 0; | |
| 120 for (; it != end; ++it) { | |
| 121 MetricsMap::iterator found = metrics.find(it->name()); | |
| 122 | |
| 123 // make sure we found it, and that it's unmolested in value | |
| 124 EXPECT_TRUE(found != metrics.end() && equals(found->second, *it)); | |
| 125 count++; | |
| 126 } | |
| 127 | |
| 128 // Did we visit all metrics? | |
| 129 EXPECT_EQ(count, metrics.size()); | |
| 130 } | |
| OLD | NEW |