var Memcached;
var MemcachedMock;
var net = require('net');
var rewire = require('rewire');
var CredentialsProvider = require('../../lib/credentials/CredentialsProvider.js').CredentialsProvider;

describe("Memcached", function () {
    var connection = new CredentialsProvider({
            hosts: [
                {
                    host: "localhost",
                    port: 5522
                }
            ],
        }),
        memcached,
        endSpy;


    beforeEach(function () {
        MemcachedMock = rewire('../../lib/client/Memcached.js');
        MemcachedMock.__set__('Socket', function () {
            this._cb = null;
            this._connected = true;
            this._refineHandlers = function () {};
            this.write = function (buf, enc, cb) {
                if (this._cb) {
                    this._cb(new Buffer("END\r\n"));
                }
                if (cb) {
                    cb();
                }
            };
            this.on = function (ev, cb) {
                this._cb = cb;
            };
        });

        endSpy = jasmine.createSpy();
    });

    it("require", function () {
        Memcached = MemcachedMock.Memcached;

        expect(Memcached).toBeTruthy();
    });

    it("set", function () {
        runs(function () {
            memcached = new Memcached(connection);
            var tmp = memcached.set("eloszka", {
                data: "1234",
                length: 4
            }, endSpy);
        });

        waitsFor(function () {
            return endSpy.callCount == 1;
        });

        runs(function () {
            expect(endSpy.argsForCall[0][1]).toBeTruthy();
            memcached.destroy();
        });
    });

    it("get", function () {
        runs(function () {
            memcached = new Memcached(connection);
            var tmp = memcached.get("eloszka", null, endSpy);
        });

        waitsFor(function () {
            return endSpy.callCount == 1;
        });

        runs(function () {
            expect(endSpy.argsForCall[0][1]).toBeNull();
        });
    });
});
