text:
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/service_worker/service_worker_disk_cache_migrator.h"

#include <string>
#include <vector>

#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/thread_task_runner_handle.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_storage.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/http/http_response_headers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

namespace {

const int kMaxDiskCacheSize = 250 * 1024 * 1024;

struct ResponseData {
  int64 resource_id;
  std::string headers;
  std::string body;
  std::string metadata;

  ResponseData(int64 resource_id,
               const std::string& headers,
               const std::string& body,
               const std::string& metadata)
      : resource_id(resource_id),
        headers(headers),
        body(body),
        metadata(metadata) {}
};

void OnDiskCacheMigrated(const base::Closure& callback,
                         ServiceWorkerStatusCode status) {
  EXPECT_EQ(SERVICE_WORKER_OK, status);
  callback.Run();
}

void OnRegistrationFound(
    const base::Closure& callback,
    ServiceWorkerStatusCode status,
    const scoped_refptr<ServiceWorkerRegistration>& registration) {
  callback.Run();
}

}  // namespace

class ServiceWorkerDiskCacheMigratorTest : public testing::Test {
 public:
  ServiceWorkerDiskCacheMigratorTest()
      : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}

  void SetUp() override {
    ASSERT_TRUE(user_data_directory_.CreateUniqueTempDir());
    scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager(
        new MockServiceWorkerDatabaseTaskManager(
            base::ThreadTaskRunnerHandle::Get()));

    context_.reset(new ServiceWorkerContextCore(
        user_data_directory_.path(), database_task_manager.Pass(),
        base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr, nullptr,
        nullptr));
  }

  void TearDown() override {
    context_.reset();
    base::RunLoop().RunUntilIdle();
  }

  scoped_ptr<ServiceWorkerDiskCache> CreateSrcDiskCache() {
#if defined(OS_ANDROID)
    // Android has already used the Simple backend.
    scoped_ptr<ServiceWorkerDiskCache> src(
        ServiceWorkerDiskCache::CreateWithSimpleBackend());
#else
    scoped_ptr<ServiceWorkerDiskCache> src(
        ServiceWorkerDiskCache::CreateWithBlockFileBackend());
#endif  // defined(OS_ANDROID)

    net::TestCompletionCallback cb;
    src->InitWithDiskBackend(
        storage()->GetOldDiskCachePath(), kMaxDiskCacheSize, false /* force */,
        base::ThreadTaskRunnerHandle::Get(), cb.callback());
    EXPECT_EQ(net::OK, cb.WaitForResult());
    return src.Pass();
  }

  scoped_ptr<ServiceWorkerDiskCache> CreateDestDiskCache() {
    scoped_ptr<ServiceWorkerDiskCache> dest(
        ServiceWorkerDiskCache::CreateWithSimpleBackend());
    net::TestCompletionCallback cb;
    dest->InitWithDiskBackend(
        storage()->GetDiskCachePath(), kMaxDiskCacheSize, false /* force */,
        base::ThreadTaskRunnerHandle::Get(), cb.callback());
    EXPECT_EQ(net::OK, cb.WaitForResult());
    return dest.Pass();
  }

  scoped_ptr<ServiceWorkerDiskCacheMigrator> CreateMigrator() {
    return make_scoped_ptr(new ServiceWorkerDiskCacheMigrator(
        storage()->GetOldDiskCachePath(), storage()->GetDiskCachePath(),
        kMaxDiskCacheSize, base::ThreadTaskRunnerHandle::Get()));
  }

  bool WriteResponse(ServiceWorkerDiskCache* disk_cache,
                     const ResponseData& response) {
    scoped_ptr<ServiceWorkerResponseWriter> writer(
        new ServiceWorkerResponseWriter(response.resource_id, disk_cache));

    // Write the response info.
    scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo);
    info->request_time = base::Time() + base::TimeDelta::FromSeconds(10);
    info->response_time = info->request_time + base::TimeDelta::FromSeconds(10);
    info->was_cached = false;
    info->headers = new net::HttpResponseHeaders(response.headers);
    scoped_refptr<HttpResponseInfoIOBuffer> info_buffer =
        new HttpResponseInfoIOBuffer(info.release());
    net::TestCompletionCallback cb1;
    writer->WriteInfo(info_buffer.get(), cb1.callback());
    int rv = cb1.WaitForResult();
    EXPECT_LT(0, rv);
    if (rv < 0)
      return false;

    // Write the response metadata.
    scoped_ptr<ServiceWorkerResponseMetadataWriter> metadata_writer(
        new ServiceWorkerResponseMetadataWriter(response.resource_id,
                                                disk_cache));
    scoped_refptr<net::IOBuffer> metadata_buffer(
        new net::WrappedIOBuffer(response.metadata.data()));
    const int metadata_length = response.metadata.length();
    net::TestCompletionCallback cb2;
    metadata_writer->WriteMetadata(metadata_buffer.get(), metadata_length,
                                   cb2.callback());
    rv = cb2.WaitForResult();
    EXPECT_EQ(metadata_length, rv);
    if (metadata_length != rv)
      return false;

    // Write the response body.
    scoped_refptr<net::IOBuffer> body_buffer(
        new net::WrappedIOBuffer(response.body.data()));
    const int body_length = response.body.length();
    net::TestCompletionCallback cb3;
    writer->WriteData(body_buffer.get(), body_length, cb3.callback());
    rv = cb3.WaitForResult();
    EXPECT_EQ(body_length, rv);
    if (body_length != rv)
      return false;

    return true;
  }

  void VerifyResponse(ServiceWorkerDiskCache* disk_cache,
                      const ResponseData& expected) {
    scoped_ptr<ServiceWorkerResponseReader> reader(
        new ServiceWorkerResponseReader(expected.resource_id, disk_cache));

    // Verify the response info.
    scoped_refptr<HttpResponseInfoIOBuffer> info_buffer =
        new HttpResponseInfoIOBuffer;
    net::TestCompletionCallback cb1;
    reader->ReadInfo(info_buffer.get(), cb1.callback());
    int rv = cb1.WaitForResult();
    EXPECT_LT(0, rv);
    ASSERT_TRUE(info_buffer->http_info);
    ASSERT_TRUE(info_buffer->http_info->headers);
    EXPECT_EQ("OK", info_buffer->http_info->headers->GetStatusText());

    // Verify the response metadata.
    if (!expected.metadata.empty()) {
      ASSERT_TRUE(info_buffer->http_info->metadata);
      const int data_size = info_buffer->http_info->metadata->size();
      ASSERT_EQ(static_cast<int>(expected.metadata.length()), data_size);
      EXPECT_EQ(0, memcmp(expected.metadata.data(),
                          info_buffer->http_info->metadata->data(),
                          expected.metadata.length()));
    }

    // Verify the response body.
    const int kBigEnough = 512;
    scoped_refptr<net::IOBuffer> body_buffer = new net::IOBuffer(kBigEnough);
    net::TestCompletionCallback cb2;
    reader->ReadData(body_buffer.get(), kBigEnough, cb2.callback());
    rv = cb2.WaitForResult();
    ASSERT_EQ(static_cast<int>(expected.body.length()), rv);
    EXPECT_EQ(0, memcmp(expected.body.data(), body_buffer->data(), rv));
  }

  int32 GetEntryCount(ServiceWorkerDiskCache* disk_cache) {
    return disk_cache->disk_cache()->GetEntryCount();
  }

  ServiceWorkerStorage* storage() { return context_->storage(); }

 private:
  TestBrowserThreadBundle browser_thread_bundle_;
  base::ScopedTempDir user_data_directory_;

  scoped_ptr<ServiceWorkerContextCore> context_;
};

TEST_F(ServiceWorkerDiskCacheMigratorTest, MigrateDiskCache) {
  std::vector<ResponseData> responses;
  responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", ""));
  responses.push_back(ResponseData(2, "HTTP/1.1 200 OK\0\0", "Service", ""));
  responses.push_back(ResponseData(5, "HTTP/1.1 200 OK\0\0", "Worker", ""));
  responses.push_back(ResponseData(3, "HTTP/1.1 200 OK\0\0", "World", "meta"));
  responses.push_back(ResponseData(10, "HTTP/1.1 200 OK\0\0", "", "meta"));
  responses.push_back(ResponseData(11, "HTTP/1.1 200 OK\0\0", "body", ""));
  responses.push_back(ResponseData(12, "HTTP/1.1 200 OK\0\0", "", ""));
  responses.push_back(ResponseData(
      20, "HTTP/1.1 200 OK\0\0", std::string(256, 'a'), std::string(128, 'b')));

  // Populate initial data in the src diskcache.
  scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache());
  for (const ResponseData& response : responses) {
    ASSERT_TRUE(WriteResponse(src.get(), response));
    VerifyResponse(src.get(), response);
  }
  ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
  src.reset();

  // Start the migrator.
  base::RunLoop run_loop;
  scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator(CreateMigrator());
  migrator->Start(base::Bind(&OnDiskCacheMigrated, run_loop.QuitClosure()));
  run_loop.Run();

  // Verify the migrated contents in the dest diskcache.
  scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache());
  for (const ResponseData& response : responses)
    VerifyResponse(dest.get(), response);
  EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest.get()));
}

TEST_F(ServiceWorkerDiskCacheMigratorTest, MigrateEmptyDiskCache) {
  scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache());
  ASSERT_EQ(0, GetEntryCount(src.get()));
  src.reset();

  // Start the migrator.
  base::RunLoop run_loop;
  scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator(CreateMigrator());
  migrator->Start(base::Bind(&OnDiskCacheMigrated, run_loop.QuitClosure()));
  run_loop.Run();

  scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache());
  ASSERT_EQ(0, GetEntryCount(dest.get()));
}

// Tests that the migrator properly removes existing resources in the dest
// diskcache before starting the migration.
TEST_F(ServiceWorkerDiskCacheMigratorTest, RemoveExistingResourcesFromDest) {
  std::vector<ResponseData> responses1;
  responses1.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", ""));
  responses1.push_back(ResponseData(3, "HTTP/1.1 200 OK\0\0", "World", ""));

  std::vector<ResponseData> responses2;
  responses2.push_back(ResponseData(10, "HTTP/1.1 200 OK\0\0", "Hello", ""));
  responses2.push_back(ResponseData(11, "HTTP/1.1 200 OK\0\0", "Service", ""));
  responses2.push_back(ResponseData(12, "HTTP/1.1 200 OK\0\0", "", "Worker"));

  // Populate initial resources in the src diskcache.
  scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache());
  for (const ResponseData& response : responses1) {
    ASSERT_TRUE(WriteResponse(src.get(), response));
    VerifyResponse(src.get(), response);
  }
  ASSERT_EQ(static_cast<int>(responses1.size()), GetEntryCount(src.get()));
  src.reset();

  // Populate different resources in the dest diskcache in order to simulate
  // a previous partial migration.
  scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache());
  for (const ResponseData& response : responses2) {
    ASSERT_TRUE(WriteResponse(dest.get(), response));
    VerifyResponse(dest.get(), response);
  }
  ASSERT_EQ(static_cast<int>(responses2.size()), GetEntryCount(dest.get()));
  dest.reset();

  // Start the migrator.
  base::RunLoop run_loop;
  scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator(CreateMigrator());
  migrator->Start(base::Bind(&OnDiskCacheMigrated, run_loop.QuitClosure()));
  run_loop.Run();

  // Verify that only newly migrated resources exist in the dest diskcache.
  dest = CreateDestDiskCache();
  for (const ResponseData& response : responses1)
    VerifyResponse(dest.get(), response);
  EXPECT_EQ(static_cast<int>(responses1.size()), GetEntryCount(dest.get()));
}

TEST_F(ServiceWorkerDiskCacheMigratorTest, ThrottleInflightTasks) {
  std::vector<ResponseData> responses;
  for (int i = 0; i < 10; ++i)
    responses.push_back(ResponseData(i, "HTTP/1.1 200 OK\0\0", "foo", "bar"));

  // Populate initial data in the src diskcache.
  scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache());
  for (const ResponseData& response : responses) {
    ASSERT_TRUE(WriteResponse(src.get(), response));
    VerifyResponse(src.get(), response);
  }
  ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
  src.reset();

  scoped_ptr<ServiceWorkerDiskCacheMigrator> migrator(CreateMigrator());

  // Tighten the max number of inflight tasks.
  migrator->set_max_number_of_inflight_tasks(2);

  // Migration should hit the limit, but should successfully complete.
  base::RunLoop run_loop;
  migrator->Start(base::Bind(&OnDiskCacheMigrated, run_loop.QuitClosure()));
  run_loop.Run();

  // Verify the migrated contents in the dest diskcache.
  scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache());
  for (const ResponseData& response : responses)
    VerifyResponse(dest.get(), response);
  EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest.get()));
}

TEST_F(ServiceWorkerDiskCacheMigratorTest, MigrateOnDiskCacheAccess) {
  std::vector<ResponseData> responses;
  responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", ""));
  responses.push_back(ResponseData(2, "HTTP/1.1 200 OK\0\0", "Service", ""));
  responses.push_back(ResponseData(5, "HTTP/1.1 200 OK\0\0", "Worker", ""));
  responses.push_back(ResponseData(3, "HTTP/1.1 200 OK\0\0", "World", "meta"));

  // Populate initial resources in the src diskcache.
  scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache());
  for (const ResponseData& response : responses) {
    ASSERT_TRUE(WriteResponse(src.get(), response));
    VerifyResponse(src.get(), response);
  }
  ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
  ASSERT_TRUE(base::DirectoryExists(storage()->GetOldDiskCachePath()));
  src.reset();

  scoped_ptr<ServiceWorkerDatabase> database(
      new ServiceWorkerDatabase(storage()->GetDatabasePath()));

  // This is necessary to make the storage schedule diskcache migration.
  database->set_skip_writing_diskcache_migration_state_on_init_for_testing();

  // Simulate an existing database.
  std::vector<ServiceWorkerDatabase::ResourceRecord> resources;
  resources.push_back(ServiceWorkerDatabase::ResourceRecord(
      1, GURL("https://example.com/foo"), 10));
  ServiceWorkerDatabase::RegistrationData deleted_version;
  std::vector<int64> newly_purgeable_resources;
  ServiceWorkerDatabase::RegistrationData data;
  data.registration_id = 100;
  data.scope = GURL("https://example.com/");
  data.script = GURL("https://example.com/script.js");
  data.version_id = 200;
  data.resources_total_size_bytes = 10;
  ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK,
            database->WriteRegistration(data, resources, &deleted_version,
                                        &newly_purgeable_resources));
  database.reset();

  // LazyInitialize() reads initial data and should schedule to migrate.
  ASSERT_FALSE(storage()->disk_cache_migration_needed_);
  base::RunLoop run_loop;
  storage()->LazyInitialize(run_loop.QuitClosure());
  run_loop.Run();
  EXPECT_TRUE(storage()->disk_cache_migration_needed_);

  // DiskCache access should start the migration.
  ServiceWorkerDiskCache* dest = storage()->disk_cache();

  // Verify the migrated contents in the dest diskcache.
  for (const ResponseData& response : responses)
    VerifyResponse(dest, response);
  EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(dest));

  // After the migration, the src diskcache should be deleted.
  EXPECT_FALSE(base::DirectoryExists(storage()->GetOldDiskCachePath()));

  // After the migration, the migration state should be updated.
  bool migration_needed = false;
  EXPECT_EQ(
      ServiceWorkerDatabase::STATUS_OK,
      storage()->database_->IsDiskCacheMigrationNeeded(&migration_needed));
  EXPECT_FALSE(migration_needed);
  bool deletion_needed = false;
  EXPECT_EQ(
      ServiceWorkerDatabase::STATUS_OK,
      storage()->database_->IsOldDiskCacheDeletionNeeded(&deletion_needed));
  EXPECT_FALSE(deletion_needed);
}

TEST_F(ServiceWorkerDiskCacheMigratorTest, NotMigrateOnDatabaseAccess) {
  std::vector<ResponseData> responses;
  responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", ""));
  responses.push_back(ResponseData(2, "HTTP/1.1 200 OK\0\0", "Service", ""));
  responses.push_back(ResponseData(5, "HTTP/1.1 200 OK\0\0", "Worker", ""));
  responses.push_back(ResponseData(3, "HTTP/1.1 200 OK\0\0", "World", "meta"));

  // Populate initial resources in the src diskcache.
  scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache());
  for (const ResponseData& response : responses) {
    ASSERT_TRUE(WriteResponse(src.get(), response));
    VerifyResponse(src.get(), response);
  }
  ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
  ASSERT_TRUE(base::DirectoryExists(storage()->GetOldDiskCachePath()));

  scoped_ptr<ServiceWorkerDatabase> database(
      new ServiceWorkerDatabase(storage()->GetDatabasePath()));

  // This is necessary to make the storage schedule diskcache migration.
  database->set_skip_writing_diskcache_migration_state_on_init_for_testing();

  // Simulate an existing database.
  std::vector<ServiceWorkerDatabase::ResourceRecord> resources;
  resources.push_back(ServiceWorkerDatabase::ResourceRecord(
      1, GURL("https://example.com/foo"), 10));
  ServiceWorkerDatabase::RegistrationData deleted_version;
  std::vector<int64> newly_purgeable_resources;
  ServiceWorkerDatabase::RegistrationData data;
  data.registration_id = 100;
  data.scope = GURL("https://example.com/");
  data.script = GURL("https://example.com/script.js");
  data.version_id = 200;
  data.resources_total_size_bytes = 10;
  ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK,
            database->WriteRegistration(data, resources, &deleted_version,
                                        &newly_purgeable_resources));
  database.reset();

  // LazyInitialize() reads initial data and should schedule to migrate.
  ASSERT_FALSE(storage()->disk_cache_migration_needed_);
  base::RunLoop run_loop1;
  storage()->LazyInitialize(run_loop1.QuitClosure());
  run_loop1.Run();
  EXPECT_TRUE(storage()->disk_cache_migration_needed_);

  // Database access should not start the migration.
  base::RunLoop run_loop2;
  storage()->FindRegistrationForDocument(
      GURL("http://example.com/"),
      base::Bind(&OnRegistrationFound, run_loop2.QuitClosure()));
  run_loop2.Run();

  // Verify that the migration didn't happen.
  scoped_ptr<ServiceWorkerDiskCache> dest(CreateDestDiskCache());
  EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
  EXPECT_EQ(0, GetEntryCount(dest.get()));
  EXPECT_TRUE(base::DirectoryExists(storage()->GetOldDiskCachePath()));
}

TEST_F(ServiceWorkerDiskCacheMigratorTest, NotMigrateForEmptyDatabase) {
  std::vector<ResponseData> responses;
  responses.push_back(ResponseData(1, "HTTP/1.1 200 OK\0\0", "Hello", ""));
  responses.push_back(ResponseData(2, "HTTP/1.1 200 OK\0\0", "Service", ""));
  responses.push_back(ResponseData(5, "HTTP/1.1 200 OK\0\0", "Worker", ""));
  responses.push_back(ResponseData(3, "HTTP/1.1 200 OK\0\0", "World", "meta"));

  // Populate initial resources in the src diskcache.
  scoped_ptr<ServiceWorkerDiskCache> src(CreateSrcDiskCache());
  for (const ResponseData& response : responses) {
    ASSERT_TRUE(WriteResponse(src.get(), response));
    VerifyResponse(src.get(), response);
  }
  ASSERT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
  ASSERT_TRUE(base::DirectoryExists(storage()->GetOldDiskCachePath()));
  src.reset();

  // LazyInitialize() reads initial data and should not schedule to migrate
  // because the database is empty.
  ASSERT_FALSE(storage()->disk_cache_migration_needed_);
  base::RunLoop run_loop;
  storage()->LazyInitialize(run_loop.QuitClosure());
  run_loop.Run();
  EXPECT_FALSE(storage()->disk_cache_migration_needed_);

  // DiskCache access should not start the migration.
  ServiceWorkerDiskCache* dest = storage()->disk_cache();

  // Verify that the migration didn't happen.
  src = CreateSrcDiskCache();
  for (const ResponseData& response : responses)
    VerifyResponse(src.get(), response);
  EXPECT_EQ(static_cast<int>(responses.size()), GetEntryCount(src.get()));
  EXPECT_TRUE(base::DirectoryExists(storage()->GetOldDiskCachePath()));
  EXPECT_EQ(0, GetEntryCount(dest));

  // Write a registration into the database to start database initialization.
  std::vector<ServiceWorkerDatabase::ResourceRecord> resources;
  resources.push_back(ServiceWorkerDatabase::ResourceRecord(
      1, GURL("https://example.com/foo"), 10));
  ServiceWorkerDatabase::RegistrationData deleted_version;
  std::vector<int64> newly_purgeable_resources;
  ServiceWorkerDatabase::RegistrationData data;
  data.registration_id = 100;
  data.scope = GURL("https://example.com/");
  data.script = GURL("https://example.com/script.js");
  data.version_id = 200;
  data.resources_total_size_bytes = 10;
  ASSERT_EQ(ServiceWorkerDatabase::STATUS_OK,
            storage()->database_->WriteRegistration(
                data, resources, &deleted_version, &newly_purgeable_resources));

  // After the database initialization, the migration state should be
  // initialized.
  bool migration_needed = false;
  EXPECT_EQ(
      ServiceWorkerDatabase::STATUS_OK,
      storage()->database_->IsDiskCacheMigrationNeeded(&migration_needed));
  EXPECT_FALSE(migration_needed);
  bool deletion_needed = false;
  EXPECT_EQ(
      ServiceWorkerDatabase::STATUS_OK,
      storage()->database_->IsOldDiskCacheDeletionNeeded(&deletion_needed));

  // The deletion flag should be set for the case that the database is empty but
  // the old diskcache exists.
  EXPECT_TRUE(deletion_needed);
}

}  // namespace content


text:
package node

import (
	"context"

	"github.com/pkg/errors"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	clientset "k8s.io/client-go/kubernetes"
	bootstraputil "k8s.io/cluster-bootstrap/token/util"

	bootstraptokenv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/bootstraptoken/v1"
	"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
)

// CreateNewTokens tries to create a token and fails if one with the same ID already exists
func CreateNewTokens(client clientset.Interface, tokens []bootstraptokenv1.BootstrapToken) error {
	return UpdateOrCreateTokens(client, true, tokens)
}

// UpdateOrCreateTokens attempts to update a token with the given ID, or create if it does not already exist.
func UpdateOrCreateTokens(client clientset.Interface, failIfExists bool, tokens []bootstraptokenv1.BootstrapToken) error {

	for _, token := range tokens {

		secretName := bootstraputil.BootstrapTokenSecretName(token.Token.ID)
		secret, err := client.CoreV1().Secrets(metav1.NamespaceSystem).Get(context.TODO(), secretName, metav1.GetOptions{})
		if secret != nil && err == nil && failIfExists {
			return errors.Errorf("a token with id %q already exists", token.Token.ID)
		}

		updatedOrNewSecret := bootstraptokenv1.BootstrapTokenToSecret(&token)
		// Try to create or update the token with an exponential backoff
		err = apiclient.TryRunCommand(func() error {
			if err := apiclient.CreateOrUpdateSecret(client, updatedOrNewSecret); err != nil {
				return errors.Wrapf(err, "failed to create or update bootstrap token with name %s", secretName)
			}
			return nil
		}, 5)
		if err != nil {
			return err
		}
	}
	return nil
}

redpajama_set_name: RedPajamaGithub


text:
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
<!ENTITY % BOOK_ENTITIES SYSTEM "cloudstack.ent">
%BOOK_ENTITIES;
]>

  <title>Reconfiguring Networks in VMs</title>
  <para>&PRODUCT; provides you the ability to move VMs between networks and reconfigure a VM's
    network. You can remove a VM from a network and add to a new network. You can also change the
    default network of a virtual machine. With this functionality, hybrid or traditional server
    loads can be accommodated with ease. </para>
  <para>This feature is supported on XenServer, VMware, and KVM hypervisors.</para>

    <title>Prerequisites</title>
    <para>Ensure that vm-tools are running on guest VMs for adding or removing networks to work on
      VMware hypervisor.</para>

    <title>Adding a Network</title>
    <orderedlist>
      <listitem>
        <para>Log in to the &PRODUCT; UI as an administrator or end user.</para>
      </listitem>
      <listitem>
        <para>In the left navigation, click Instances.</para>
      </listitem>
      <listitem>
        <para>Choose the VM that you want to work with.</para>
      </listitem>
      <listitem>
        <para>Click the NICs tab.</para>
      </listitem>
      <listitem>
        <para>Click Add network to VM.</para>
        <para>The Add network to VM dialog is displayed.</para>
      </listitem>
      <listitem>
        <para>In the drop-down list, select the network that you would like to add this VM
          to.</para>
        <para> A new NIC is added for this network. You can view the following details in the NICs
          page:</para>
        <itemizedlist>
          <listitem>
            <para>ID</para>
          </listitem>
          <listitem>
            <para>Network Name</para>
          </listitem>
          <listitem>
            <para>Type</para>
          </listitem>
          <listitem>
            <para>IP Address</para>
          </listitem>
          <listitem>
            <para>Gateway</para>
          </listitem>
          <listitem>
            <para>Netmask</para>
          </listitem>
          <listitem>
            <para>Is default</para>
          </listitem>
          <listitem>
            <para>CIDR (for IPv6)</para>
          </listitem>
        </itemizedlist>
      </listitem>
    </orderedlist>

    <title>Removing a Network</title>
    <orderedlist>
      <listitem>
        <para>Log in to the &PRODUCT; UI as an administrator or end user.</para>
      </listitem>
      <listitem>
        <para>In the left navigation, click Instances.</para>
      </listitem>
      <listitem>
        <para>Choose the VM that you want to work with.</para>
      </listitem>
      <listitem>
        <para>Click the NICs tab.</para>
      </listitem>
      <listitem>
        <para>Locate the NIC you want to remove.</para>
      </listitem>
      <listitem>
        <para>Click Remove NIC button.<inlinemediaobject>
          <imageobject>
            <imagedata fileref="./images/remove-nic.png"/>
          </imageobject>
          <textobject>
              <phrase>remove-nic.png: button to remove a NIC</phrase>
            </textobject>
        </inlinemediaobject></para>
      </listitem>
      <listitem>
        <para>Click Yes to confirm.</para>
      </listitem>
    </orderedlist>

    <title>Selecting the Default Network</title>
    <orderedlist>
      <listitem>
        <para>Log in to the &PRODUCT; UI as an administrator or end user.</para>
      </listitem>
      <listitem>
        <para>In the left navigation, click Instances.</para>
      </listitem>
      <listitem>
        <para>Choose the VM that you want to work with.</para>
      </listitem>
      <listitem>
        <para>Click the NICs tab.</para>
      </listitem>
      <listitem>
        <para>Locate the NIC you want to work with.</para>
      </listitem>
      <listitem>
        <para>Click the Set default NIC button.<inlinemediaobject>
          <imageobject>
            <imagedata fileref="./images/set-default-nic.png"/>
          </imageobject>
          <textobject>
              <phrase>set-default-nic.png: button to set a NIC as default one.</phrase>
            </textobject>
        </inlinemediaobject></para>
      </listitem>
      <listitem>
        <para>Click Yes to confirm.</para>
      </listitem>
    </orderedlist>


text:
""" Simple program to print distances between atoms """

from Bio.PDB.PDBParser import PDBParser
import numpy as np

parser = PDBParser()

st = parser.get_structure('1UBQ', '1ubq.pdb')

#Selection of Residue 10 of Chain A
res10 = st[0]["A"][10]
#Selection of Residue 20 of Chain A
res20 = st[0]["A"][20]

print("Residue 10 is", res10.get_resname())
print("Residue 20 is", res20.get_resname())

print("\nAtom1 Atom2 dist1 dist2\n-------------------------")
for at10 in res10.get_atoms():      # Replace get_atoms with get_atom if you get an Error!
    for at20 in res20.get_atoms():
        dist = at20 - at10     # Direct procedure with (-) to compute distances
        vector = at20.coord - at10.coord  # Or using numpy coordinates
        distance = np.sqrt(np.sum(vector ** 2))
        print(at10, at20, dist, distance)

center = np.array([10, 10, 10])
print("\nDistance of res10 to {} \n".format(center))
for at10 in res10.get_atoms():
    vect = at10.coord - center
    distance = np.sqrt(np.sum(vect ** 2))
    print(at10, distance)


text:
module E'6''7 where

import Pictures ( Picture )

import Data.List ( intercalate )

-- We need to place a line break after each line of a picture.
-- Hoogle: http://www.haskell.org/hoogle/?hoogle=[[a]]+-%3E+[a]+-%3E+[[a]]
-- I searched for [[a]] -> [a] -> [[a]] and it told me, that Data.List.intercalate
-- would come in handy.

printPicture :: Picture -> IO ()
printPicture picture

 =  putStr ( (intercalate "\n" picture) ++ "\n" )

-- Other solution for "printPicture":

printPicture' :: Picture -> IO ()
printPicture' picture

 =  putStr brokenLines

 where

 lineBreak :: String
 lineBreak =  "\n"

 brokenLines :: String
 brokenLines =  [ currentChar | line <- picture , currentChar <- (line ++ lineBreak) ]


text:
package yts.module;

import org.junit.Ignore;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import yts.response.YtsResponse;
import yts.response.YtsStatus;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class RequestModuleTest extends ModuleTest {

    @Ignore
    @Test
    public void testCreateSuccess() {
        Map<String, Object> options = new HashMap<String, Object>();
        options.put("request_message", "This is a testing request, please disregard");

        YtsResponse response = CLIENT.requests()
                .create(APPLICATION_KEY,
                        USER_KEY,
                        "Test movie Title",
                        options);

        assertNotNull(response);
        assertEquals(YtsStatus.OK, response.status);
    }
}


text:
package org.apache.camel.opentracing.integration;

import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import io.opentracing.contrib.grizzly.ahc.TracingRequestFilter;
import io.opentracing.tag.Tags;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.opentracing.CamelOpenTracingTestSupport;
import org.apache.camel.opentracing.SpanTestData;
import org.apache.camel.test.AvailablePortFinder;
import org.junit.jupiter.api.Test;

public class InterprocessIT extends CamelOpenTracingTestSupport {
    private static final String URL = "http://localhost:" + AvailablePortFinder.getNextAvailable() + "/test";
    private static final String URI = "jetty:" + URL;

    private static SpanTestData[] testdata = {
            new SpanTestData().setLabel("jetty:start server").setUri(URI).setOperation("GET")
                    .setKind("server")
                    .addTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER)
                    .addTag(Tags.COMPONENT.getKey(), "camel-jetty")
                    .addTag(Tags.HTTP_URL.getKey(), URL)
                    .addTag(Tags.HTTP_METHOD.getKey(), "GET")
                    // jetty does not set the http response code header
                    // this is where instrumentation has value
                    // .addTag(Tags.HTTP_STATUS.getKey(), "200")
                    .setParentId(1),
            new SpanTestData().setLabel("ahc: client").setUri(null).setOperation("HTTP::GET")
                    .setKind("client")
                    .addTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
                    .addTag(Tags.COMPONENT.getKey(), "java-grizzly-ahc")
                    .addTag(Tags.HTTP_URL.getKey(), URL)
                    .addTag(Tags.HTTP_METHOD.getKey(), "GET")
                    .addTag(Tags.HTTP_STATUS.getKey(), "200")
    };

    public InterprocessIT() {
        super(testdata);
    }

    @Test
    public void testBridgeEndpoint() throws Exception {
        AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder()
                .addRequestFilter(new TracingRequestFilter(getTracer()))
                .build();

        try (AsyncHttpClient client = new AsyncHttpClient(config)) {
            client.prepareGet(URL).execute().get();
        }

        verify();
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                Processor serviceProc = new Processor() {
                    public void process(Exchange exchange) throws Exception {
                        // get the request URL and copy it to the request body
                        String uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
                        exchange.getMessage().setBody(uri);
                    }
                };

                from(URI).process(serviceProc);
            }
        };
    }
}


text:
/**
 * Provides a tool to create symlinks on HDFS
 */
import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;

class Symlink {
	public static void main(String args[]) throws Exception {
		FileContext ctx = FileContext.getFileContext();

		if (args.length < 2) {
			System.out.println("Symlink <src> <src> ... <tgt>");
		}

		String tgt = args[args.length - 1];
		for (int i = 0; i < args.length - 1; i++) {
			String src = args[i];
			ctx.createSymlink(new Path(src), new Path(tgt), false);
		}
	}
}


text:
/**
 * Controller for the Resource Modal form. The Resource Modal form manages CRUD operations for the Resource collection.
 * The form utilizes the angular bootstrap ui Modal directive.
 * Data is passed to the controller via the 'modalParams' object injected into the controller. The object must have
 * two parameters:
 *    modalParams.mode - A string value starting with 'C', 'R', 'U', or 'D'. This determines the operational mode
 *                       of the form and is not case sensitive.
 *    modalParams.data - A property that identifies the instance of the Resource collection to work with. This is the
 *                       id of the item in the case of 'R', 'U' or 'D' operations.
 *    modalParams.extraData - An optional object containing Resource data that will be placed in specific properties
 *                            when creating a new instance (mode 'C'). The object property names match the Resource
 *                            properties to pre populate.
 *
 * For 'C', 'U' and 'D' operations, successful completion, after pressing the save button, the form will display a
 * success message then automatically close after a pre-defined delay. The form will return the new or updated
 * instance object. In the case of a delete operation, the _id of the deleted object will be returned.
 *
 * The form is activated, and any returned results are handled by the following code:
 *        let modalInstance = $modal.open({
 *                     templateUrl: './templates/resourceFormModal.html',
 *                     controller: 'resourceFormModalCtrl',
 *                     size: size,
 *                     resolve: {
 *                       modalParams: function () {
 *                         return {data: '', mode: 'Create'};
 *                       }
 *                     }
 *                   });
 *
 *        modalInstance.result.then(function (result) {
 *                     // handle result which is the instance of the Resource collection the modal form worked with.
 *                     // in the case of the delete ('d' mode), the id of the deleted instance is returned.
 *                   });
 *
 */
define(['./module'], function (controllers) {
  'use strict';

  controllers.controller('ResourceFormModalCtrl', ['$scope',
    '$modalInstance',
    'modalParams',
    'Resource',
    'configService',
    'modalUtility',
    'dbEnums',
    function ($scope, $modalInstance, modalParams, Resource, configService, utility, dbEnums) {
      console.log("ResourceFormModal controller fired");

      let helpers = new utility.Helpers($scope, $modalInstance);
      $scope.actionMsg = '';
      $scope.saveTxt = configService.loctxt.add;
      $scope.cancelTxt = configService.loctxt.cancel;
      $scope.txt = configService.loctxt;
      $scope.deleteMode = false;
      $scope.confirmed = false;

      //Since we must manipulate the decimal number fields (convert from de to en numbers), we can't connect to the DB model directly
      $scope.decimals = {
        price: undefined
      };

      // Build arrays for the select boxes
      $scope.resourceTypes = dbEnums.getResourceTypeEnum();

      // Determine CRUD mode of form.
      // For all but 'C' the query can be by id or by the firm_name property which is also unique.
      const mode = modalParams.mode.substring(0, 1).toLowerCase();
      const qry = {
        '_id': modalParams.data
      };
      const notFound = configService.loctxt.resource + ' "' + modalParams.data + '" ' + configService.loctxt.notFound;

      switch (mode) {
        case 'c':
          $scope.title = configService.loctxt.resource_titleCreate;
          $scope.resource = new Resource();
          if ($scope.resourceTypes.length === 1) {
            $scope.resource.resource_type = $scope.resourceTypes[0]; //default to first type, we will not make this field editable
          }
          _prePopulateItem(modalParams.extraData);
          $scope.edit = true;
          $scope.read = false;
          break;
        case 'r':
          $scope.title = configService.loctxt.resource_titleRead;
          Resource.findOne(qry).then((resource) => {
            if (resource) {
              $scope.resource = resource;
              $scope.edit = false;
              $scope.read = true;
              $scope.cancelTxt = configService.loctxt.close;
              helpers.dApply();
            } else {
              helpers.showLoadErr(notFound);
            }
          }).catch(err => helpers.showLoadErr(err));
          break;
        case 'u':
          $scope.title = configService.loctxt.resource_titleUpdate;
          Resource.findOne(qry).then((resource) => {
            if (resource) {
              $scope.resource = resource;
              $scope.decimals.price = resource.price;
              $scope.edit = true;
              $scope.read = false;
              $scope.saveTxt = configService.loctxt.update;
              helpers.dApply();
            } else {
              helpers.showLoadErr(notFound);
            }
          }).catch(err => helpers.showLoadErr(err));
          break;
        case 'd':
          $scope.title = configService.loctxt.resource_titleDelete;
          Resource.findOne(qry).then((resource) => {
            if (resource) {
              $scope.resource = resource;
              $scope.edit = false;
              $scope.read = true;
              $scope.deleteMode = true;
              $scope.cancelTxt = configService.loctxt.close;
              $scope.saveTxt = configService.loctxt.delete;
            } else {
              helpers.showLoadErr(notFound);
            }
          }).catch(err => helpers.showLoadErr(err));
          break;
      }

      // listen to changes in the decimal number fields text variables and match them up with the model's properties
      $scope.$watch('decimals.price', function (newval) {
        if (newval) {
          $scope.resource.price = Number(newval); // string already converted to decimal representation by directive on input.
        }
      });

      // modal button click methods
      // save button handler
      $scope.save = function () {
        //perform any pre save form validation or logic here
        let msg = '';
        //save/update Resource and return
        $scope.resource.save().then(() => {
          msg = (mode === 'c' ? configService.loctxt.resource + configService.loctxt.success_saved :
            configService.loctxt.success_changes_saved);
          helpers.autoClose(msg, $scope.resource);
        }).catch((err) => helpers.showSaveError(err));
      };

      // Delete btn handler
      $scope.delete = function (err) {
        let id = $scope.resource._id;
        $scope.err = false;
        $scope.errSave = false;
        $scope.resource.remove().then(() => {
          let msg = configService.loctxt.expenseItem + configService.loctxt.success_deleted;
          helpers.autoClose(msg, id);
        }).catch(err => helpers.showSaveError(err));
      };

      //# region - private functions
      /**
       * populates the Resource model's fields from matching fields in the fieldsObj object.
       * @param {*} fieldsObj
       */
      function _prePopulateItem(fieldsObj) {
        if (fieldsObj) {
          for (let prop in fieldsObj) {
            if (fieldsObj.hasOwnProperty(prop)) {
              $scope.resource[prop] = fieldsObj[prop];
            }
          }
        }
      }
    }
  ]);
});


text:
from __future__ import unicode_literals

from django.db import models, migrations
import django.db.models.deletion
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Query',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('title', models.CharField(max_length=255)),
                ('sql', models.TextField()),
                ('description', models.TextField(null=True, blank=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('last_run_date', models.DateTimeField(auto_now=True)),
                ('created_by_user', models.ForeignKey(blank=True, to=settings.AUTH_USER_MODEL, null=True)),
            ],
            options={
                'ordering': ['title'],
                'verbose_name_plural': 'Queries',
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='QueryLog',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('sql', models.TextField()),
                ('is_playground', models.BooleanField(default=False)),
                ('run_at', models.DateTimeField(auto_now_add=True)),
                ('query', models.ForeignKey(on_delete=django.db.models.deletion.SET_NULL, blank=True, to='explorer.Query', null=True)),
                ('run_by_user', models.ForeignKey(blank=True, to=settings.AUTH_USER_MODEL, null=True)),
            ],
            options={
                'ordering': ['-run_at'],
            },
            bases=(models.Model,),
        ),
    ]


text:
from numpy.testing import assert_array_equal

from astropy import modeling
from astropy.modeling.math_functions import __all__ as math_classes
from astropy.modeling.math_functions import *
from astropy.modeling import math_functions
from .basic import TransformType

__all__ = ['NpUfuncType']

class NpUfuncType(TransformType):
    name = "transform/math_functions"
    version = '1.0.0'
    types = ['astropy.modeling.math_functions.'+ kl for kl in math_classes]

    @classmethod
    def from_tree_transform(cls, node, ctx):
        klass_name = math_functions._make_class_name(node['func_name'])
        klass = getattr(math_functions, klass_name)
        return klass()

    @classmethod
    def to_tree_transform(cls, model, ctx):
        return {'func_name': model.func.__name__}


text:
<?php

use yii\db\Migration;

class m160202_062627_lang_image extends Migration
{
    public function up()
    {
        $this->addColumn('lang', 'image_id', $this->integer());
        $this->addForeignKey('fk_lang_image_id__image_id', 'lang', 'image_id', 'image', 'id', 'SET NULL', 'CASCADE');
    }

    public function down()
    {
        $this->dropColumn('lang', 'image_id');
    }

    /*
    // Use safeUp/safeDown to run migration code within a transaction
    public function safeUp()
    {
    }

    public function safeDown()
    {
    }
    */
}


text:
"""
GPU data alignment.

Some web sites say that maximizing performance for OpenCL code requires
aligning data on certain memory boundaries.  The following functions
provide this service:

:func:`align_data` aligns an existing array, returning a new array of the
correct alignment.

:func:`align_empty` to create an empty array of the correct alignment.

Set alignment to gpu environment attribute *boundary*.

Note:  This code is unused. So far, tests have not demonstrated any
improvement from forcing correct alignment.  The tests should
be repeated with arrays forced away from the target boundaries
to decide whether it is really required.
"""
import numpy as np  # type: ignore

def align_empty(shape, dtype, alignment=128):
    """
    Return an empty array aligned on the alignment boundary.
    """
    size = np.prod(shape)
    dtype = np.dtype(dtype)
    # allocate array with extra space for alignment
    extra = alignment//dtype.itemsize - 1
    result = np.empty(size+extra, dtype)
    # build a view into allocated array which starts on a boundary
    offset = (result.ctypes.data%alignment)//dtype.itemsize
    view = np.reshape(result[offset:offset+size], shape)
    return view

def align_data(x, dtype, alignment=128):
    """
    Return a copy of an array on the alignment boundary.
    """
    # if x is contiguous, aligned, and of the correct type then just return x
    view = align_empty(x.shape, dtype, alignment=alignment)
    view[:] = x
    return view


text:
package com.yahoo.sketches.hll;

import static java.nio.charset.StandardCharsets.UTF_8;

import org.testng.Assert;
import org.testng.annotations.Test;

public class SingleCouponMapTest {

  @Test
  public void getEstimateNoEntry() {
    SingleCouponMap map = SingleCouponMap.getInstance(1000, 4);
    byte[] key = new byte[] {0, 0, 0, 1};
    Assert.assertEquals(map.getEstimate(key), 0.0);
    Assert.assertEquals(map.getUpperBound(key), 0.0);
    Assert.assertEquals(map.getLowerBound(key), 0.0);
  }

  @Test
  public void oneKeyOneEntry() {
    int entries = 16;
    int keySizeBytes = 4;
    SingleCouponMap map = SingleCouponMap.getInstance(entries, keySizeBytes);
    byte[] key = new byte[] {0, 0, 0, 0}; // zero key must work
    byte[] id =  new byte[] {1, 0, 0, 0};
    short coupon = (short) Map.coupon16(id);
    double estimate = map.update(key, coupon);
    Assert.assertEquals(estimate, 1.0);
    Assert.assertEquals(map.getEstimate(key), 1.0);
    Assert.assertTrue(map.getUpperBound(key) >= 1.0);
    Assert.assertTrue(map.getLowerBound(key) <= 1.0);
  }

  @Test
  public void resize() {
    int entries = 17;
    int numKeys = 1000;
    int keySizeBytes = 4;
    SingleCouponMap map = SingleCouponMap.getInstance(entries, keySizeBytes);

    for (int i = 0; i < numKeys; i++) {
      byte[] key = String.format("%4s", i).getBytes(UTF_8);
      byte[] id =  new byte[] {1, 0, 0, 0};
      short coupon = (short) Map.coupon16(id);
      double estimate = map.update(key, coupon);
      Assert.assertEquals(estimate, 1.0);
      Assert.assertEquals(map.getEstimate(key), 1.0);
      Assert.assertTrue(map.getUpperBound(key) >= 1.0);
      Assert.assertTrue(map.getLowerBound(key) <= 1.0);
    }
    for (int i = 0; i < numKeys; i++) {
      byte[] key = String.format("%4s", i).getBytes(UTF_8);
      Assert.assertEquals(map.getEstimate(key), 1.0);
      Assert.assertTrue(map.getUpperBound(key) >= 1.0);
      Assert.assertTrue(map.getLowerBound(key) <= 1.0);
    }
    println(map.toString());
    Assert.assertEquals(map.getCurrentCountEntries(), numKeys);
  }

  @Test
  public void manyKeys() {
    SingleCouponMap map = SingleCouponMap.getInstance(2000, 4);
    for (int i = 1; i <= 1000; i++) {
      byte[] key = String.format("%4s", i).getBytes(UTF_8);
      double estimate = map.update(key, (short) 1); //bogus coupon
      Assert.assertEquals(estimate, 1.0);
      Assert.assertEquals(map.getEstimate(key), 1.0);
      Assert.assertTrue(map.getUpperBound(key) >= 1.0);
      Assert.assertTrue(map.getLowerBound(key) <= 1.0);
    }
    for (int i = 1; i <= 1000; i++) {
      byte[] key = String.format("%4s", i).getBytes(UTF_8);
      double estimate = map.update(key, (short) 2); //different bogus coupon
      Assert.assertEquals(estimate, 0.0); // signal to promote
    }
    Assert.assertEquals(map.getCurrentCountEntries(), 1000);
  }

  @Test
  public void printlnTest() {
    println("PRINTING: "+this.getClass().getName());
  }

  /**
   * @param s value to print
   */
  static void println(String s) {
    //System.out.println(s); //disable here
  }

}


text:
FROM node:7.2
MAINTAINER iottly

RUN mkdir /iottly_console
#ADD /iottly_console /iottly_console

WORKDIR /iottly_console

RUN npm install -g grunt-cli bower yo generator-karma generator-angular --verbose

USER node

CMD /bin/bash


text:
import './vendor.ts';

import { NgModule, Injector } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {HTTP_INTERCEPTORS, HttpClientModule} from '@angular/common/http';
import { Ng2Webstorage, LocalStorageService, SessionStorageService  } from 'ngx-webstorage';
import { JhiEventManager } from 'ng-jhipster';

import { AuthInterceptor } from './blocks/interceptor/auth.interceptor';
import { AuthExpiredInterceptor } from './blocks/interceptor/auth-expired.interceptor';
import { ErrorHandlerInterceptor } from './blocks/interceptor/errorhandler.interceptor';
import { NotificationInterceptor } from './blocks/interceptor/notification.interceptor';
import { TheBuybackSharedModule, UserRouteAccessService } from './shared';
import { TheBuybackHomeModule } from './home/home.module';
import { TheBuybackAdminModule } from './admin/admin.module';
import { TheBuybackAccountModule } from './account/account.module';
import { TheBuybackEntityModule } from './entities/entity.module';
import { PaginationConfig } from './blocks/config/uib-pagination.config';
// jhipster-needle-angular-add-module-import JHipster will add new module here
import {
    JhiMainComponent,
    NavbarComponent,
    FooterComponent,
    ProfileService,
    PageRibbonComponent,
    ErrorComponent
} from './layouts';
import {SsoComponent} from "./layouts/callback/sso.component";
import {TheBuybackTheBuybackModule} from "./the-buyback/the-buyback.module";
import {LoginComponent} from "./layouts/login/login.component";
import {TheBuybackOrderCapsModule} from "./order-caps/order-caps.module";
import {LayoutRoutingModule} from "./app-routing.module";
import {TheBuybackDoctrineStockModule} from "./doctrine-stock/doctrine-stock.module";
import {TheBuybackItemStockModule} from "./item-stock/item-stock.module";
import {HttpModule} from "@angular/http";

@NgModule({
    imports: [
        BrowserModule,
        LayoutRoutingModule,
        // https://github.com/angular/angular/issues/20096#issuecomment-341374051
        HttpModule,
        HttpClientModule,
        Ng2Webstorage.forRoot({ prefix: 'jhi', separator: '-'}),
        TheBuybackSharedModule,
        TheBuybackHomeModule,
        TheBuybackAdminModule,
        TheBuybackAccountModule,
        TheBuybackEntityModule,
        TheBuybackTheBuybackModule,
        TheBuybackOrderCapsModule,
        TheBuybackDoctrineStockModule,
        TheBuybackItemStockModule
        // jhipster-needle-angular-add-module JHipster will add new module here
    ],
    declarations: [
        JhiMainComponent,
        NavbarComponent,
        ErrorComponent,
        PageRibbonComponent,
        FooterComponent,
        SsoComponent,
        LoginComponent
    ],
    providers: [
        ProfileService,
        PaginationConfig,
        UserRouteAccessService,
        {
            provide: HTTP_INTERCEPTORS,
            useClass: AuthInterceptor,
            multi: true,
            deps: [
                LocalStorageService,
                SessionStorageService
            ]
        },
        {
            provide: HTTP_INTERCEPTORS,
            useClass: AuthExpiredInterceptor,
            multi: true,
            deps: [
                Injector
            ]
        },
        {
            provide: HTTP_INTERCEPTORS,
            useClass: ErrorHandlerInterceptor,
            multi: true,
            deps: [
                JhiEventManager
            ]
        },
        {
            provide: HTTP_INTERCEPTORS,
            useClass: NotificationInterceptor,
            multi: true,
            deps: [
                Injector
            ]
        }
    ],
    bootstrap: [ JhiMainComponent ]
})
export class TheBuybackAppModule {}


text:
from xlrd import open_workbook
from xlrd import sheet
import os

from comicmodels.models import ProjectLink

class ProjectExcelReader(object):
    """ Records on challenges from grand-challenge.org are kept in an xls file
    This had the advantages of easy copy-pasting and quick simple and flexible
    editing and statistics. This class reads the grand-challenge xls file and
    renders html for each challenge found.

    TODO: This class combines functionality which should probably be separate:
    read xls, render projectlink to html etc. Split later.
    """

    def __init__(self,path,sheetname):
        self.path = path
        self.sheetname = sheetname

    def write_link_html(self,path=""):
        items = self.get_excel_items(self.sheet)
        links = self.get_project_links().encode("utf-8")

        if path == "":
            path = "D:/temp/links.html"

        f = open(path,"w")
        print ("* wrote HTML for all projects to "+path)
        f.write(links)
        f.close()

    def get_project_links(self):
        """ Read excel file and with challenge listings and return an array of
        projectlinks describing each
        """

        book = open_workbook(self.path)
        sheet = book.sheet_by_name(self.sheetname)

        html = ""
        items = self.get_excel_items(sheet)
        projectlinks = []

        for item in items:
            if item["abreviation"] != "":
                projectlink = ProjectLink(item)
                projectlinks.append(projectlink)

        book.unload_sheet(self.sheetname)
        return projectlinks

    def get_project_links_html(self):
        """ Read excel file and output html to show all challenge links
        listed there
        """

        html = ""
        projectlinks = self.get_project_links()

        for projectlink in projectlinks:
            html += projectlink.render_to_html()

        return html

    def get_excel_items(self,sheet):
        """ Treat each row in excel sheet as an item. First row in sheet should
        contain column headers. Each item returned is an object that has a field
        for each column.

        """
        items = []

        for row in range(1,sheet.nrows): #skip first row as those are titles
            items.append(self.get_excel_item(sheet,row))
        return items

    def get_excel_item(self,sheet,row):
        """
        """
        item = {}

        col_titles = sheet.row_values(0,0)
        col_values = sheet.row_values(row,0)
        for (title,value) in zip(col_titles,col_values):

            if title != "":
                item[title] = value

        return item


text:
/* eslint-disable import/no-extraneous-dependencies */
const { createDefaultConfig } = require('@open-wc/testing-karma');
const merge = require('deepmerge');

module.exports = (config) => {
  config.set(
      merge(createDefaultConfig(config), {
        files: [
          // runs all files ending with .test in the test folder,
          // can be overwritten by passing a --grep flag. examples:
          //
          // npm run test -- --grep test/foo/bar.test.js
          // npm run test -- --grep test/bar/*
          {
            pattern: config.grep ? config.grep : 'test/**/*.test.js',
            type: 'module',
          },
        ],

        // see the karma-esm docs for all options
        esm: {
          // if you are using 'bare module imports' you will need this option
          nodeResolve: true
        }
      })
  );
  return config;
};


text:
<Record>
<Term>Nplate</Term>
<SemanticType>Pharmacologic Substance</SemanticType>
<SemanticType>Organic Chemical</SemanticType>
<Synonym>Romiplostim</Synonym>
<Synonym>Nplate</Synonym>
<Source>FDA Registry</Source>
</Record>


text:
//this source code was auto-generated by tolua#, do not modify it
using System;
using LuaInterface;

public class System_Collections_IEnumeratorWrap
{
	public static void Register(LuaState L)
	{
		L.BeginClass(typeof(System.Collections.IEnumerator), null);
		L.RegFunction("MoveNext", MoveNext);
		L.RegFunction("Reset", Reset);
		L.RegVar("Current", get_Current, null);
		L.EndClass();
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int MoveNext(IntPtr L)
	{
		try
		{
			ToLua.CheckArgsCount(L, 1);
			System.Collections.IEnumerator obj = ToLua.CheckIter(L, 1);
			bool o = obj.MoveNext();
			LuaDLL.lua_pushboolean(L, o);
			return 1;
		}
		catch (Exception e)
		{
			return LuaDLL.toluaL_exception(L, e);
		}
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int Reset(IntPtr L)
	{
		try
		{
			ToLua.CheckArgsCount(L, 1);
			System.Collections.IEnumerator obj = ToLua.CheckIter(L, 1);
			obj.Reset();
			return 0;
		}
		catch (Exception e)
		{
			return LuaDLL.toluaL_exception(L, e);
		}
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int get_Current(IntPtr L)
	{
		object o = null;

		try
		{
			o = ToLua.ToObject(L, 1);
			System.Collections.IEnumerator obj = (System.Collections.IEnumerator)o;
			object ret = obj.Current;
			ToLua.Push(L, ret);
			return 1;
		}
		catch(Exception e)
		{
			return LuaDLL.toluaL_exception(L, e, o, "attempt to index Current on a nil value");
		}
	}
}


text:
#if !defined(BOOST_SPIRIT_ARGUMENT_MARCH_22_2011_0939PM)
#define BOOST_SPIRIT_ARGUMENT_MARCH_22_2011_0939PM

#include <boost/spirit/include/phoenix_core.hpp>

namespace boost { namespace spirit
{
    template <int N>
    struct argument;

    template <typename Dummy>
    struct attribute_context;

    namespace expression
    {
#ifndef BOOST_SPIRIT_USE_PHOENIX_V3
        template <int N>
        struct argument
        {
            typedef phoenix::actor<spirit::argument<N> > type;

            static type make()
            {
                return spirit::argument<N>();
            }
        };

        template <typename Dummy>
        struct attribute_context
        {
            typedef phoenix::actor<spirit::attribute_context<Dummy> > type;

            static type make()
            {
                return spirit::attribute_context<Dummy>();
            }
        };
#else
        template <int N>
        struct argument
          : phoenix::expression::terminal<spirit::argument<N> >
        {
            typedef typename phoenix::expression::terminal<
                spirit::argument<N>
            >::type type;

            static type make()
            {
                type const e = {{{}}};
                return e;
            }
        };

        template <typename Dummy>
        struct attribute_context
          : phoenix::expression::terminal<spirit::attribute_context<Dummy> >
        {
            typedef typename phoenix::expression::terminal<
                spirit::attribute_context<Dummy>
            >::type type;

            static type make()
            {
                type const e = {{{}}};
                return e;
            }
        };
#endif
    }
}}

#ifdef BOOST_SPIRIT_USE_PHOENIX_V3
namespace boost { namespace phoenix
{
    namespace result_of
    {
        template <typename Dummy>
        struct is_nullary<custom_terminal<spirit::attribute_context<Dummy> > >
          : mpl::false_
        {};

        template <int N>
        struct is_nullary<custom_terminal<spirit::argument<N> > >
          : mpl::false_
        {};
    }

    template <typename Dummy>
    struct is_custom_terminal<spirit::attribute_context<Dummy> >
      : mpl::true_
    {};

    template <int N>
    struct is_custom_terminal<spirit::argument<N> >
      : mpl::true_
    {};

    template <typename Dummy>
    struct custom_terminal<spirit::attribute_context<Dummy> >
      : proto::call<
            v2_eval(
                proto::make<spirit::attribute_context<Dummy>()>
              , proto::call<
                    functional::env(proto::_state)
                >
            )
        >
    {};

    template <int N>
    struct custom_terminal<spirit::argument<N> >
      : proto::call<
            v2_eval(
                proto::make<spirit::argument<N>()>
              , proto::call<
                    functional::env(proto::_state)
                >
            )
        >
    {};
}}
#endif // BOOST_SPIRIT_USE_PHOENIX_V3

#endif


text:
<Alloy>
	<View class="container">
		<Label id="message"></Label>
	</View>
</Alloy>


text:
package org.cloudname.backends.zookeeper;

import com.google.common.base.Charsets;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.cloudname.core.CloudnameBackend;
import org.cloudname.core.CloudnamePath;
import org.cloudname.core.LeaseHandle;
import org.cloudname.core.LeaseListener;
import org.cloudname.core.LeaseType;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * A ZooKeeper backend for Cloudname. Leases are represented as nodes; client leases are ephemeral
 * nodes inside container nodes and permanent leases are container nodes.
 *
 * @author stalehd@gmail.com
 */
public class ZooKeeperBackend implements CloudnameBackend {
    private static final Logger LOG = Logger.getLogger(ZooKeeperBackend.class.getName());
    private static final String ZK_ROOT = "/cn/";
    private static final int CONNECTION_TIMEOUT_SECONDS = 30;

    private final CuratorFramework curator;
    private final Map<LeaseListener, NodeCollectionWatcher> collectionListeners = new HashMap<>();
    private final Map<LeaseListener, NodeCollectionWatcher> leaseListeners = new HashMap<>();
    private final Object syncObject = new Object();

    /**
     * @param connectionString ZooKeeper connection string
     * @throws IllegalStateException if the cluster isn't available.
     */
    public ZooKeeperBackend(final String connectionString) {
        final RetryPolicy retryPolicy = new ExponentialBackoffRetry(200, 10);
        curator = CuratorFrameworkFactory.newClient(connectionString, retryPolicy);
        curator.start();

        try {
            curator.blockUntilConnected(CONNECTION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
            LOG.info("Connected to zk cluster @ " + connectionString);
        } catch (final InterruptedException ie) {
            throw new IllegalStateException("Could not connect to ZooKeeper", ie);
        }
    }

    @Override
    public boolean writeLeaseData(final CloudnamePath path, final String data) {
        final String zkPath = ZK_ROOT + path.join('/');
        try {
            final Stat nodeStat = curator.checkExists().forPath(zkPath);
            if (nodeStat == null) {
                LOG.log(Level.WARNING, "Could not write client lease data for " + path
                        + " with data since the path does not exist. Data = " + data);
            }
            curator.setData().forPath(zkPath, data.getBytes(Charsets.UTF_8));
            return true;
        } catch (final Exception ex) {
            LOG.log(Level.WARNING, "Got exception writing lease data to " + path
                    + " with data " + data);
            return false;
        }
    }

    @Override
    public String readLeaseData(final CloudnamePath path) {
        if (path == null) {
            return null;
        }
        final String zkPath = ZK_ROOT + path.join('/');
        try {
            curator.sync().forPath(zkPath);
            final byte[] bytes = curator.getData().forPath(zkPath);
            return new String(bytes, Charsets.UTF_8);
        } catch (final Exception ex) {
            LOG.log(Level.WARNING, "Got exception reading client lease data at " + path, ex);
        }
        return null;
    }

    private CloudnamePath toCloudnamePath(final String zkPath) {
        final String clientPath = zkPath.substring(ZK_ROOT.length());
        final String[] elements = clientPath.split("/");
        return new CloudnamePath(elements);
    }

    @Override
    public void addLeaseCollectionListener(
            final CloudnamePath pathToObserve, final LeaseListener listener) {
        // Ideally the PathChildrenCache class in Curator would be used here to keep track of the
        // changes but it is ever so slightly broken and misses most of the watches that ZooKeeper
        // triggers, ignores the mzxid on the nodes and generally makes a mess of things. Enter
        // custom code.
        final String zkPath = ZK_ROOT + pathToObserve.join('/');
        try {
            curator.createContainers(zkPath);
            final NodeCollectionWatcher watcher = new NodeCollectionWatcher(
                    curator.getZookeeperClient().getZooKeeper(),
                    zkPath,
                    new NodeWatcherListener() {

                        @Override
                        public void nodeCreated(final String path, final String data) {
                            listener.leaseCreated(toCloudnamePath(path), data);
                        }

                        @Override
                        public void dataChanged(final String path, final String data) {
                            listener.dataChanged(toCloudnamePath(path), data);
                        }

                        @Override
                        public void nodeRemoved(final String path) {
                            listener.leaseRemoved(toCloudnamePath(path));
                        }
                    });

            synchronized (syncObject) {
                collectionListeners.put(listener, watcher);
            }
        } catch (final Exception exception) {
            LOG.log(Level.WARNING, "Got exception when creating node watcher", exception);
        }
    }

    @Override
    public void addLeaseListener(final CloudnamePath leaseToObserve, final LeaseListener listener) {
        try {
            final String parentPath = ZK_ROOT + leaseToObserve.getParent().join('/');
            final String fullPath = ZK_ROOT + leaseToObserve.join('/');
            curator.createContainers(parentPath);
            final NodeCollectionWatcher watcher = new NodeCollectionWatcher(
                    curator.getZookeeperClient().getZooKeeper(),
                    parentPath,
                    new NodeWatcherListener() {

                        @Override
                        public void nodeCreated(final String path, final String data) {
                            if (path.equals(fullPath)) {
                                listener.leaseCreated(toCloudnamePath(path), data);
                            }
                        }

                        @Override
                        public void dataChanged(final String path, final String data) {
                            if (path.equals(fullPath)) {
                                listener.dataChanged(toCloudnamePath(path), data);
                            }
                        }

                        @Override
                        public void nodeRemoved(final String path) {
                            if (path.equals(fullPath)) {
                                listener.leaseRemoved(toCloudnamePath(path));
                            }
                        }
                    });

            synchronized (syncObject) {
                leaseListeners.put(listener, watcher);
            }
        } catch (final Exception exception) {
            LOG.log(Level.WARNING, "Got exception when creating node watcher", exception);
        }
    }

    @Override
    public void removeLeaseListener(final LeaseListener listener) {
        synchronized (syncObject) {
            final NodeCollectionWatcher collectionWatcher = collectionListeners.get(listener);
            if (collectionWatcher != null) {
                collectionListeners.remove(listener);
                collectionWatcher.shutdown();
            }
            final NodeCollectionWatcher leaseWatcher = leaseListeners.get(listener);
            if (leaseWatcher != null) {
                leaseListeners.remove(listener);
                leaseWatcher.shutdown();
            }
        }
    }

    @Override
    public LeaseHandle createLease(
            final LeaseType type, final CloudnamePath path, final String data) {
        if (type == null || path == null || data == null) {
            return null;
        }

        final String zkPath = ZK_ROOT + path.join('/');
        try {
            curator.sync().forPath(zkPath);
            final Stat nodeStat = curator.checkExists().forPath(zkPath);
            if (nodeStat == null) {
                final CreateMode mode = (type == LeaseType.PERMANENT
                        ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL);

                final String returnedPath = curator.create()
                        .creatingParentContainersIfNeeded()
                        .withMode(mode)
                        .forPath(zkPath, data.getBytes(Charsets.UTF_8));

                if (returnedPath == null) {
                    LOG.warning("Could not create node for path " + path
                            + " - Curator returned null on create()");
                    return null;
                }
                return new LeaseHandle() {
                    private AtomicBoolean closed = new AtomicBoolean(false);

                    @Override
                    public boolean writeData(final String data) {
                        if (closed.get()) {
                            LOG.info("Attempt to write data to closed leased handle " + data);
                            return false;
                        }
                        return writeLeaseData(path, data);
                    }

                    @Override
                    public CloudnamePath getLeasePath() {
                        if (closed.get()) {
                            return null;
                        }
                        return path;
                    }

                    @Override
                    public void close() throws IOException {
                        if (type == LeaseType.PERMANENT || closed.get()) {
                            return;
                        }
                        try {
                            curator.delete().forPath(zkPath);
                            closed.set(true);
                        } catch (final Exception ex) {
                            throw new IOException(ex);
                        }
                    }
                };
            }

            LOG.log(Level.INFO, "Attempt to create node at " + path
                    + " with data " + data + " but it already exists");

        } catch (final Exception ex) {
            LOG.log(Level.WARNING, "Got exception creating parent container for lease"
                    + " for lease " + path + " with data " + data, ex);
        }
        return null;
    }

    @Override
    public boolean removeLease(final CloudnamePath path) {
        final String zkPath = ZK_ROOT + path.join('/');
        try {
            final Stat nodeStat = curator.checkExists().forPath(zkPath);
            if (nodeStat != null) {
                curator.delete()
                        .withVersion(nodeStat.getVersion())
                        .forPath(zkPath);
                return true;
            }
            return false;
        } catch (final Exception ex) {
            LOG.log(Level.WARNING, "Got error removing node for lease " + path, ex);
            return false;
        }
    }

    @Override
    public void close() {
        synchronized (syncObject) {
            collectionListeners.values().forEach(NodeCollectionWatcher::shutdown);
            collectionListeners.clear();
            leaseListeners.values().forEach(NodeCollectionWatcher::shutdown);
            leaseListeners.clear();
        }
    }
}


text:
using UnityEngine;
using System.Collections;

public class CostContainer : MonoBehaviour {
    UILabel label;
	// Use this for initialization
    void Awake()
    {
        label = transform.FindChild("Label").GetComponent<UILabel>();
	}

    public void SetCost(int cost)
    {
        if (cost == 0)
        {
            label.text = "";
        }
        else
        {
            label.text = cost.ToString();
        }
    }
}


text:
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;

[CustomEditor(typeof(vp_InputMobile))]
public class vp_InputMobileEditor : Editor
{

	// target component
	public vp_InputMobile m_Component;

	/// <summary>
	/// hooks up the component object as the inspector target
	/// </summary>
	public virtual void OnEnable()
	{

		m_Component = (vp_InputMobile)target;

	}

	/// <summary>
	///
	/// </summary>
	public override void OnInspectorGUI()
	{

		GUI.color = Color.white;

		GUILayout.Space(10);
		GUILayout.BeginHorizontal();
		GUILayout.Space(10);
		if (GUILayout.Button("Open VP Input Manager", GUILayout.MinWidth(150), GUILayout.MinHeight(25)))
			vp_InputWindow.Init();
		GUILayout.Space(10);
		GUILayout.EndHorizontal();
		GUILayout.Space(10);

		// update
		if (GUI.changed)
		{

			EditorUtility.SetDirty(target);

		}

	}

}


text:
using System;
using System.Collections.Generic;

namespace HonjoLib
{
    public interface IBladeExpressionEvaluator
    {
        string Evaluate(string expression, List<Type> types, List<Tuple<string, Type>> namedTypes);
    }
}


text:
<?php

namespace ApplicationTest\Entity;

use Application\Entity\Message;
use Application\Entity\User;
use \PHPUnit_Framework_TestCase;

class MessageTest extends PHPUnit_Framework_TestCase
{
    /**
     * Methode setUp
     */
    public function setUp() {
        $this->instance = new Message();
    }

    /**
     * Methode tearDown
     */
    public function tearDown() {
        $this->instance = null;
    }

    /**
     * Test Get Set Content
     */
    public function testGetSetContent() {
        $fixture = 'myContent';

        $this->assertSame($this->instance,
            $this->instance->setContent($fixture));

        $this->assertSame($fixture, $this->instance->getContent());
    }

    /**
     * Test Get Input Filter
     */
    public function testGetInputFilter() {

        $inputFilterMock = $this->getMock('Zend\InputFilter\InputFilter');

        $this->setInaccessiblePropertyValue(
            'inputFilter',
            $inputFilterMock);

        $this->assertSame($inputFilterMock,
            $this->instance->getInputFilter());
    }

    /*+
     * Test Get Set Date Time
     */
    public function testGetSetPostTime() {

        $fixture = '1111';

        $this->assertSame($this->instance,
            $this->instance->setPostTime($fixture));

        $this->assertSame($fixture, $this->instance->getPostTime());
    }

    /*+
     * Test Get Set Title
     */
    public function testGetSetTitle() {
        $fixture = 'myTitle';

        $this->assertSame($this->instance,
            $this->instance->setTitle($fixture));

        $this->assertSame($fixture, $this->instance->getTitle());
    }

    /*+
     * Test Get Set Date Time
     */
    public function testGetSetUser() {

        $fixture = new User();

        $this->assertSame($this->instance,
            $this->instance->setUser($fixture));

        $this->assertSame($fixture, $this->instance->getUser());
    }

    /**
     * Test Get Set Date Time Exception
     */
    public function testGetSetUserException() {

        $fixture = 'notAUserObject';

        $this->setExpectedException('Exception');

        $this->instance->setUser($fixture);
    }
}


text:
import {
    COMMIT,
    MERGE,
    CHECKOUT,
    REVERT,
} from './types';

export default function createReducer(registry) {
    function createState() {
        return {
            heads: registry.reduce((previous, repository, repositoryName) => ({
                ...previous,
                [repositoryName]: repository.head,
            }), {}),
        };
    }

    const initialState = createState();

    return function reducer(state = initialState, action) {
        const {
            payload: {
                repositoryName,
                ...payload
            } = {},
            type,
        } = action;

        if (!repositoryName) {
            return state;
        }

        if (!registry.has(repositoryName)) {
            throw new Error(`Repository ${repositoryName} not found`);
        }

        const repository = registry.get(repositoryName);

        switch (type) {
        case CHECKOUT:
            repository.checkout(
                payload.targetBranch,
                payload.shouldCreateBranch,
            );

            return createState();

        case COMMIT:
            repository.commit(
                payload.author,
                payload.message,
                payload.tree,
            );

            return createState();

        case MERGE:
            if (payload.resolver) {
                repository.merge(
                    payload.author,
                    payload.targetBranch,
                    payload.resolver,
                );
            } else {
                repository.merge(
                    payload.author,
                    payload.targetBranch,
                );
            }

            return createState();

        case REVERT:
            if (payload.resolver) {
                repository.revert(
                    payload.author,
                    payload.targetCommitHash,
                    payload.resolver,
                );
            } else {
                repository.revert(
                    payload.author,
                    payload.targetCommitHash,
                );
            }

            return createState();

        default:
            return state;
        }
    };
}


text:
#pragma once

#include <vector>

#include "paddle/phi/common/backend.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/layout.h"
#include "paddle/phi/core/ddim.h"
#include "paddle/utils/any.h"
#include "paddle/utils/optional.h"

namespace phi {

/*
 * LoD is short for Level of Details.
 *
 * - in a level, each element indicates relative offset of the lower level
 * - the first element should be 0 and that indicates that this sequence start
 * from 0
 * - each sequence's begin and end(no-inclusive) is level[id, id+1]
 *
 * For example:
 *    3-level LoD stores
 *
 *    0 2 3
 *    0 2 4 7
 *    0 2 5 7 10 12 15 20
 */
using LoD = std::vector<std::vector<size_t>>;

/// \brief The meta data of dense tensor. Take the structure type
/// and use all default operations.
///
struct DenseTensorMeta {
  using DataType = paddle::experimental::DataType;

  DenseTensorMeta();
  DenseTensorMeta(DataType dtype, const DDim& dims);
  DenseTensorMeta(DataType dtype,
                  const DDim& dims,
                  DataLayout layout,
                  size_t offset = 0);
  DenseTensorMeta(DataType dtype,
                  const DDim& dims,
                  DataLayout layout,
                  const LoD& lod,
                  size_t offset = 0);

  /// \brief Test whether the metadata is valid. Does not throw exceptions.
  /// \return Whether the metadata is valid.
  bool valid() const noexcept;

  bool is_scalar{false};
  /// \brief Determine whether using CuDNN speed-up library in the new dygraph.
  /// It maybe also support MKLDNN library in the near future.
  bool use_cudnn{true};
  DDim dims;
  DataType dtype{DataType::UNDEFINED};
  DataLayout layout{DataLayout::NCHW};
  LoD lod;
  size_t offset{0};
};

inline bool operator==(const DenseTensorMeta& lhs, const DenseTensorMeta& rhs) {
  return (lhs.is_scalar == rhs.is_scalar) && lhs.use_cudnn == rhs.use_cudnn &&
         (lhs.dims == rhs.dims) && (lhs.dtype == rhs.dtype) &&
         (lhs.layout == rhs.layout) && (lhs.lod == rhs.lod) &&
         (lhs.offset == rhs.offset);
}

struct StringTensorMeta {
  StringTensorMeta() = default;
  explicit StringTensorMeta(const DDim& dims);
  /// \brief Test whether the metadata is valid. Does not throw exceptions.
  /// \return Whether the metadata is valid.
  bool valid() const noexcept;

  /// During the entire life cycle of a DenseTensor, the following attributes
  /// marked with `const` are expected to remain unchanged.
  bool is_scalar{false};
  DDim dims;
  size_t offset{0};
};

inline bool operator==(const StringTensorMeta& lhs,
                       const StringTensorMeta& rhs) {
  return (lhs.is_scalar == rhs.is_scalar) && (lhs.dims == rhs.dims) &&
         (lhs.offset == rhs.offset);
}

struct SparseTensorMeta {
  SparseTensorMeta() = default;
  explicit SparseTensorMeta(const DDim& dims);
  explicit SparseTensorMeta(const DDim& dims, const DataLayout& layout);
  /// \brief Test whether the metadata is valid. Does not throw exceptions.
  /// \return Whether the metadata is valid.
  bool valid() const noexcept;

  DDim dims;
  DataType dtype;
  DataLayout layout{DataLayout::NCHW};
};

inline bool operator==(const SparseTensorMeta& lhs,
                       const SparseTensorMeta& rhs) {
  return (lhs.dims == rhs.dims) && (lhs.layout == rhs.layout);
}

}  // namespace phi


text:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.bogza.nodemcuide;

import java.util.Arrays;
import java.util.regex.Pattern;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.StyleContext;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

/**
 *
 * @author Constantin Bogza
 * @since 02.05.2016
 * @version 1.0.0
 *
 * Processes the text received from the NodeMCU RS232 interface and gives the
 * correct html formating for the colors and better view.
 */
public class NodeMcuTopComponentTextProcessing {
    private JTextPane jTextPaneHandle;
    private HTMLEditorKit htmlEditorKit = new HTMLEditorKit();;
    private StyleSheet htmlStyleSheet;
    private StringBuilder textPaneText = new StringBuilder();
    private byte[] dataArray = new byte[2400];
    private int dataArrayOffset;
    private JTextField jTextTempHandle;
    public NodeMcuTopComponentTextProcessing(JTextPane jTextPaneHandleParsed,JTextField jTextTempParsed)
    {
        dataArrayOffset = 0;
        jTextPaneHandle = jTextPaneHandleParsed;
        jTextTempHandle = jTextTempParsed;
        jTextPaneHandle.setContentType("text/html");
        htmlStyleSheet = htmlEditorKit.getStyleSheet();

        htmlStyleSheet.addRule("#tinel {color: red;}");
        htmlStyleSheet.addRule("#command {color: red;}");
        htmlStyleSheet.addRule("#nodeSintax {color: blue;}");
        htmlStyleSheet.addRule("#debug {color: green;}");
        jTextPaneHandle.setEditorKit(htmlEditorKit);
        textPaneText.append("NodeMcu IDE in Netbeans
© Constantin Bogza 2016
 www.bogza.ro
");
    }
    public void SendText(String textToBePrinted)
    {
        textPaneText.append(ColorString(textToBePrinted)+"
");
        jTextPaneHandle.setText(textPaneText.toString()+"");
        jTextPaneHandle.select(jTextPaneHandle.getDocument().getLength(), jTextPaneHandle.getDocument().getLength());
    }
    public void SendDebugText(String textToBePrinted)
    {
        textPaneText.append(" Debug: "+ColorString(textToBePrinted)+"
");
        jTextPaneHandle.setText(textPaneText.toString()+"");
        jTextPaneHandle.select(jTextPaneHandle.getDocument().getLength(), jTextPaneHandle.getDocument().getLength());
    }
    public String ColorString(String textToBeColored)
    {
        String tempString;
        tempString = Pattern.compile(">").matcher(textToBeColored).replaceAll(" > ");
        tempString = Pattern.compile("node").matcher(tempString).replaceAll("node");
        tempString = Pattern.compile("restart").matcher(tempString).replaceAll("restart");
        return tempString;
    }
    public void SendBytes(byte[] dataReceived)
    {
        for(int i=0; i<dataReceived.length; i++)
        {
            dataArray[dataArrayOffset]=dataReceived[i];
            dataArrayOffset++;
            if((dataReceived[i] == 10) || (dataArrayOffset > 2000))
            {
                String RS232data= new String(dataArray,0,dataArrayOffset);
                SendText(RS232data);
                dataArrayOffset = 0;
                if(dataArrayOffset > 2000) SendDebugText("Buffer overflow for the input RS232!");
            }
        }
        String RS232dataTemp= new String(dataArray,0,dataArrayOffset);
        jTextTempHandle.setText(RS232dataTemp);
    }
}


text:
/**
 * webpack打包配置文件
 */
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
    env: process.env.NODE_ENV || 'develop',

    devtool: 'inline-source-map',
    // publicPath: '/dist/',
    output: {
        path: path.resolve(__dirname, '../public/dist'),
        filename: '[name].bundle.js',
        publicPath: '/dist/'
    },
    plugins: [
        new HtmlWebpackPlugin({
            inject: 'head',
            minify: false,
            hash: true,
            chunks: ['index', 'common'],
            template: 'html-loader!views/home.ejs',
            filename: path.resolve(__dirname, '../views/_home.ejs')
        })
    ]
};


text:
'use strict';

var https = require('https');

function getLatLngFromAddress(address, callback){
  var apiEndpoint = 'https://api.t-fk.no/geocode/';

  var body = '';
  var url = apiEndpoint + address;

  https.get(url, function(res) {

    res.on('data', function(chunk) {
      body += chunk.toString();
    });

    res.on('end', function() {
      var json = JSON.parse(body);
      return callback(null, json);
    });

  }).on('error', function(error) {
    return callback(error, null);
  });
}

function getUTMFromLatLng(options, callback){
  var apiEndpoint = 'https://api.t-fk.no/geocode/convert/LLtoUTM';

  var body = '';
  var url = apiEndpoint + '?lat=' + options.lat + '&lng=' + options.lng;

  https.get(url, function(res) {

    res.on('data', function(chunk) {
      body += chunk.toString();
    });

    res.on('end', function() {
      var json = JSON.parse(body);
      return callback(null, json);
    });

  }).on('error', function(error) {
    return callback(error, null);
  });
}

function getClosestStage(options, callback){
  var apiEndpoint = 'https://api.t-fk.no/stages/closest';

  var body = '';
  var url = apiEndpoint + '?x=' + options.x + '&y=' + options.y + '&maxdistance=6000';

  https.get(url, function(res) {

    res.on('data', function(chunk) {
      body += chunk.toString();
    });

    res.on('end', function() {
      var json = JSON.parse(body);
      return callback(null, json);
    });

  }).on('error', function(error) {
    return callback(error, null);
  });
}

function getClosestStop(address, callback) {
  getLatLngFromAddress(address, function(err, data){
    if (err) {
      return callback(err, null);
    } else {

      var lat = data.results[0].geometry.location.lat;
      var lng = data.results[0].geometry.location.lng;
      getUTMFromLatLng({lat:lat, lng:lng}, function(error, position){
        if (error) {
          callback(error, null);
        } else {
          var x = position.geometry.coordinates[0];
          var y = position.geometry.coordinates[1];
          getClosestStage({x:x, y:y}, function(er, stage){
            if (er) {
              return callback(er, null);
            } else {
              return callback(null, stage);
            }
          });
        }
      })
    }
  });
}

module.exports = getClosestStop;


text:
package org.apache.samza.runtime;

import com.google.common.collect.ImmutableMap;
import org.apache.samza.SamzaException;
import org.apache.samza.config.MapConfig;
import org.apache.samza.config.TaskConfig;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import static org.mockito.Mockito.*;

public class TestClusterBasedProcessorLifecycleListener {
  private ClusterBasedProcessorLifecycleListener clusterBasedProcessorLifecycleListener;
  private ProcessorLifecycleListener processorLifecycleListener;
  private Runnable mockShutdownHookCallback;

  @Before
  public void setup() {
    mockShutdownHookCallback = mock(Runnable.class);
    processorLifecycleListener = mock(ProcessorLifecycleListener.class);
    clusterBasedProcessorLifecycleListener =
        spy(new ClusterBasedProcessorLifecycleListener(new MapConfig(ImmutableMap.of(TaskConfig.TASK_SHUTDOWN_MS, "30000")),
            processorLifecycleListener, mockShutdownHookCallback));
    doNothing().when(clusterBasedProcessorLifecycleListener).addJVMShutdownHook(any(Thread.class));
    doNothing().when(clusterBasedProcessorLifecycleListener).removeJVMShutdownHook(any(Thread.class));
  }

  @Test
  public void testLifecycleListenerBeforeStart() {
    clusterBasedProcessorLifecycleListener.beforeStart();

    Mockito.verify(clusterBasedProcessorLifecycleListener).addJVMShutdownHook(any(Thread.class));
    Mockito.verify(processorLifecycleListener).beforeStart();
  }

  @Test
  public void testLifecycleListenerAfterStart() {
    clusterBasedProcessorLifecycleListener.afterStart();
    Mockito.verify(processorLifecycleListener).afterStart();
  }

  @Test
  public void testLifecycleListenerAfterStop() {
    clusterBasedProcessorLifecycleListener.afterStop();
    Mockito.verify(processorLifecycleListener).afterStop();
    Mockito.verify(clusterBasedProcessorLifecycleListener).removeJVMShutdownHook(any(Thread.class));
  }

  @Test
  public void testLifecycleListenerAfterFailure() {
    SamzaException e = new SamzaException("Should call afterFailure");
    clusterBasedProcessorLifecycleListener.afterFailure(e);
    Mockito.verify(processorLifecycleListener).afterFailure(e);
    Mockito.verify(clusterBasedProcessorLifecycleListener).removeJVMShutdownHook(any(Thread.class));
  }

  @Test
  public void testShutdownHookInvokesShutdownHookCallback() {
    doAnswer(invocation -> {
      // Simulate call to container.shutdown()
      clusterBasedProcessorLifecycleListener.afterStop();
      return null;
    }).when(mockShutdownHookCallback).run();

    // call beforeStart to setup shutdownHook
    clusterBasedProcessorLifecycleListener.beforeStart();

    // Simulating shutdown hook invocation by JVM.
    // The shutdownHookThread should return immediately and shutdown
    // cleanly if mockShutdownHookCallback.run() is invoked.
    clusterBasedProcessorLifecycleListener.getShutdownHookThread().run();
    Mockito.verify(mockShutdownHookCallback).run();
  }
}


text:
package com.navercorp.pinpoint.profiler.util;

import org.junit.Assert;
import org.junit.Test;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.util.Date;

/**
 * @author emeroad
 */
public class RuntimeMXBeanUtilsTest {

    @Test
    public void vmStartTime() {
        long vmStartTime = RuntimeMXBeanUtils.getVmStartTime();
        Assert.assertNotSame(0, vmStartTime);
    }

    @Test
    public void pid() {
        int pid = RuntimeMXBeanUtils.getPid();
        Assert.assertTrue(pid > 0);
    }
}