From b95ca9f18e29f4ee2c9b8b90bffbc04daf0611b5 Mon Sep 17 00:00:00 2001 From: vng Date: Mon, 4 Mar 2013 21:04:52 +0300 Subject: [PATCH] Add test for apk reading in multiple threads. --- platform/platform_tests/apk_test.cpp | 128 +++++++++++++++++++++ platform/platform_tests/platform_test.cpp | 8 +- platform/platform_tests/platform_tests.pro | 3 +- 3 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 platform/platform_tests/apk_test.cpp diff --git a/platform/platform_tests/apk_test.cpp b/platform/platform_tests/apk_test.cpp new file mode 100644 index 0000000000..602d9c68b6 --- /dev/null +++ b/platform/platform_tests/apk_test.cpp @@ -0,0 +1,128 @@ +#include "../../testing/testing.hpp" + +#include "../platform.hpp" + +#include "../../coding/zip_reader.hpp" +#include "../../coding/internal/file_data.hpp" + +#include "../../base/thread.hpp" + +#include "../../std/numeric.hpp" + + +namespace +{ + char const * arrFiles[] = { + "about.html", + "resources-ldpi/basic.skn", + "resources-ldpi/plus.png", + "resources-ldpi/right-arrow.png", + "resources-ldpi/symbols.png", + "resources-mdpi/basic.skn", + "resources-mdpi/plus.png", + "resources-mdpi/right-arrow.png", + "resources-mdpi/symbols.png", + "resources-hdpi/basic.skn", + "resources-hdpi/plus.png", + "resources-hdpi/right-arrow.png", + "resources-hdpi/symbols.png", + "resources-xhdpi/basic.skn", + "resources-xhdpi/plus.png", + "resources-xhdpi/right-arrow.png", + "resources-xhdpi/symbols.png", + "categories.txt", + "classificator.txt", + "types.txt", + "fonts_blacklist.txt", + "fonts_whitelist.txt", + "languages.txt", + "unicode_blocks.txt", + "drules_proto.bin", + "external_resources.txt", + "packed_polygons.bin", + "countries.txt" + }; + + class ApkTester : public threads::IRoutine + { + static const int COUNT = ARRAY_SIZE(arrFiles); + string const & m_cont; + + public: + ApkTester(string const & cont) : m_cont(cont) + { + m_hashes.resize(COUNT); + } + + virtual void Do() + { + string const prefix("assets/"); + + while (true) + { + size_t ind = rand() % COUNT; + if (m_hashes[ind] != 0) + { + ind = COUNT; + for (size_t i = 0; i < COUNT; ++i) + if (m_hashes[i] == 0) + { + ind = i; + break; + } + } + + if (ind == COUNT) + break; + + try + { + ZipFileReader reader(m_cont, prefix + arrFiles[ind]); + + size_t const size = reader.Size(); + vector buffer(size); + reader.Read(0, &buffer[0], size); + + m_hashes[ind] = accumulate(buffer.begin(), buffer.end(), static_cast(0)); + } + catch (Reader::Exception const & ex) + { + LOG(LERROR, (ex.Msg())); + } + } + } + + vector m_hashes; + }; +} + +UNIT_TEST(ApkReader_Multithreaded) +{ + string const path = GetPlatform().WritableDir() + "../android/MapsWithMePro/bin/MapsWithMePro-production.apk"; + //string const path = "/Users/viktor/Dropbox/Public/MapsWithMePro-230-130220.apk"; + + uint64_t size; + if (!my::GetFileSize(path, size)) + { + LOG(LINFO, ("Apk not found")); + return; + } + + srand(static_cast(size)); + + size_t const count = 20; + threads::ThreadPool pool(count); + + for (size_t i = 0; i < count; ++i) + pool.Add(new ApkTester(path)); + + pool.Join(); + + typedef ApkTester const * PtrT; + PtrT etalon = dynamic_cast(pool.GetRoutine(0)); + for (size_t i = 1; i < count; ++i) + { + PtrT p = dynamic_cast(pool.GetRoutine(i)); + TEST_EQUAL(etalon->m_hashes, p->m_hashes, ()); + } +} diff --git a/platform/platform_tests/platform_test.cpp b/platform/platform_tests/platform_test.cpp index da031ac5e5..f2d290e629 100644 --- a/platform/platform_tests/platform_test.cpp +++ b/platform/platform_tests/platform_test.cpp @@ -40,7 +40,13 @@ UNIT_TEST(WritablePathForFile) UNIT_TEST(GetReader) { char const * NON_EXISTING_FILE = "mgbwuerhsnmbui45efhdbn34.tmp"; - char const * arr[] = { "drules_proto.txt", "basic_ldpi.skn", "classificator.txt", "minsk-pass.mwm" }; + char const * arr[] = { + "drules_proto.txt", + "resources-ldpi/basic.skn", + "classificator.txt", + "minsk-pass.mwm" + }; + Platform & p = GetPlatform(); for (size_t i = 0; i < ARRAY_SIZE(arr); ++i) { diff --git a/platform/platform_tests/platform_tests.pro b/platform/platform_tests/platform_tests.pro index 29bee1215e..c408603b69 100644 --- a/platform/platform_tests/platform_tests.pro +++ b/platform/platform_tests/platform_tests.pro @@ -4,7 +4,7 @@ CONFIG -= app_bundle TEMPLATE = app ROOT_DIR = ../.. -DEPENDENCIES = platform coding base tomcrypt jansson +DEPENDENCIES = platform coding base zlib tomcrypt jansson include($$ROOT_DIR/common.pri) @@ -33,3 +33,4 @@ SOURCES += \ language_test.cpp \ downloader_test.cpp \ video_timer_test.cpp \ + apk_test.cpp \