#  BSD 3-Clause License
#
#  Copyright (c) 2019, Elasticsearch BV
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#
#  * Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
#  * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
#  * Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
#  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os

import pytest

from elasticapm.conf.constants import TRANSACTION
from elasticapm.traces import capture_span

memcache = pytest.importorskip("memcache")  # isort:skip


pytestmark = [pytest.mark.memcached]


if "MEMCACHED_HOST" not in os.environ:
    pytestmark.append(pytest.mark.skip("Skipping memcached tests, no MEMCACHED_HOST environment variable set"))


@pytest.mark.integrationtest
def test_memcached(instrument, elasticapm_client):
    elasticapm_client.begin_transaction("transaction.test")
    host = os.environ.get("MEMCACHED_HOST", "localhost")
    with capture_span("test_memcached", "test"):
        conn = memcache.Client([host + ":11211"], debug=0)
        conn.set("mykey", "a")
        assert "a" == conn.get("mykey")
        assert {"mykey": "a"} == conn.get_multi(["mykey", "myotherkey"])
    elasticapm_client.end_transaction("BillingView")

    transactions = elasticapm_client.events[TRANSACTION]
    spans = elasticapm_client.spans_for_transaction(transactions[0])

    expected_signatures = {"test_memcached", "Client.set", "Client.get", "Client.get_multi"}

    assert {t["name"] for t in spans} == expected_signatures

    assert spans[0]["name"] == "Client.set"
    assert spans[0]["type"] == "cache"
    assert spans[0]["subtype"] == "memcached"
    assert spans[0]["action"] == "query"
    assert spans[0]["parent_id"] == spans[3]["id"]

    assert spans[1]["name"] == "Client.get"
    assert spans[1]["type"] == "cache"
    assert spans[1]["subtype"] == "memcached"
    assert spans[1]["action"] == "query"
    assert spans[1]["parent_id"] == spans[3]["id"]
    assert spans[1]["context"]["destination"] == {
        "address": host,
        "port": 11211,
        "service": {"name": "", "resource": "memcached", "type": ""},
    }

    assert spans[2]["name"] == "Client.get_multi"
    assert spans[2]["type"] == "cache"
    assert spans[2]["subtype"] == "memcached"
    assert spans[2]["action"] == "query"
    assert spans[2]["parent_id"] == spans[3]["id"]

    assert spans[3]["name"] == "test_memcached"
    assert spans[3]["type"] == "test"

    assert len(spans) == 4


@pytest.mark.parametrize(
    "elasticapm_client",
    [
        {
            "span_compression_enabled": True,
            "span_compression_same_kind_max_duration": "5ms",
            "span_compression_exact_match_max_duration": "50ms",
        }
    ],
    indirect=True,
)
@pytest.mark.integrationtest
def test_memcache_span_compression(instrument, elasticapm_client):
    host = os.environ.get("MEMCACHED_HOST", "localhost")
    conn = memcache.Client([host + ":11211"], debug=0)
    elasticapm_client.begin_transaction("transaction.test")
    for i in range(5):
        conn.set(str(i), i)
    elasticapm_client.end_transaction("test")
    transactions = elasticapm_client.events[TRANSACTION]
    spans = elasticapm_client.spans_for_transaction(transactions[0])
    assert len(spans) == 1
