Path: blob/main/contrib/kyua/integration/helpers/race.cpp
48199 views
// Copyright 2015 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file integration/helpers/race.cpp29/// Creates a file and reads it back, looking for races.30///31/// This program should fail with high chances if it is called multiple times at32/// once with TEST_ENV_shared_file pointing to the same file.3334extern "C" {35#include <sys/types.h>3637#include <unistd.h>38}3940#include <cstdlib>41#include <fstream>42#include <iostream>4344#include "utils/format/macros.hpp"45#include "utils/fs/operations.hpp"46#include "utils/fs/path.hpp"47#include "utils/env.hpp"48#include "utils/optional.ipp"49#include "utils/stream.hpp"5051namespace fs = utils::fs;5253using utils::optional;545556/// Entry point to the helper test program.57///58/// \return EXIT_SUCCESS if no race is detected; EXIT_FAILURE otherwise.59int60main(void)61{62const optional< std::string > shared_file = utils::getenv(63"TEST_ENV_shared_file");64if (!shared_file) {65std::cerr << "Environment variable TEST_ENV_shared_file not defined\n";66std::exit(EXIT_FAILURE);67}68const fs::path shared_path(shared_file.get());6970if (fs::exists(shared_path)) {71std::cerr << "Shared file already exists; created by a concurrent "72"test?";73std::exit(EXIT_FAILURE);74}7576const std::string contents = F("%s") % ::getpid();7778std::ofstream output(shared_path.c_str());79if (!output) {80std::cerr << "Failed to create shared file; conflict with a concurrent "81"test?";82std::exit(EXIT_FAILURE);83}84output << contents;85output.close();8687::usleep(10000);8889const std::string read_contents = utils::read_file(shared_path);90if (read_contents != contents) {91std::cerr << "Shared file contains unexpected contents; modified by a "92"concurrent test?";93std::exit(EXIT_FAILURE);94}9596fs::unlink(shared_path);97std::exit(EXIT_SUCCESS);98}99100101