package com.ctrip.xpipe.redis.integratedtest.keeper.manul;

import com.ctrip.xpipe.AbstractTest;
import com.ctrip.xpipe.client.redis.AsyncRedisClient;
import com.ctrip.xpipe.client.redis.AsyncRedisClientFactory;
import com.ctrip.xpipe.endpoint.DefaultEndPoint;
import com.ctrip.xpipe.gtid.GtidSet;
import com.ctrip.xpipe.redis.core.protocal.cmd.DefaultXsync;
import com.ctrip.xpipe.redis.core.redis.operation.RedisOp;
import com.ctrip.xpipe.redis.core.redis.operation.RedisOpParser;
import com.ctrip.xpipe.redis.core.redis.operation.RedisOpParserManager;
import com.ctrip.xpipe.redis.core.redis.operation.op.RedisOpPing;
import com.ctrip.xpipe.redis.core.redis.operation.op.RedisOpSelect;
import com.ctrip.xpipe.redis.core.redis.operation.parser.*;
import com.ctrip.xpipe.redis.core.server.FakeXsyncServer;
import com.ctrip.xpipe.redis.keeper.applier.command.ApplierRedisOpCommand;
import com.ctrip.xpipe.redis.keeper.applier.command.DefaultApplierCommand;
import com.ctrip.xpipe.redis.keeper.applier.sequence.ApplierSequenceController;
import com.ctrip.xpipe.redis.keeper.applier.sequence.DefaultSequenceController;
import org.junit.After;
import org.junit.Before;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Slight
 * <p>
 * May 29, 2022 17:28
 */
public class SequenceControllerToGtidKeeperTest extends GtidKeeperTest {

    protected RedisOpParserManager redisOpParserManager;

    protected RedisOpParser parser;

    private DefaultXsync xsync;

    private FakeXsyncServer server;

    private GtidSet gtidSet = new GtidSet("a1:1-10:15-20,b1:1-8");

    private List<RedisOp> redisOps;

    private ApplierSequenceController sequenceController;

    private AsyncRedisClient client;

    @Before
    public void setup() throws Exception {

        redisOpParserManager = new DefaultRedisOpParserManager();
        parser = new GeneralRedisOpParser(redisOpParserManager);
        new RedisOpSetParser(redisOpParserManager);
        new RedisOpMsetParser(redisOpParserManager);
        new RedisOpDelParser(redisOpParserManager);
        new RedisOpSelectParser(redisOpParserManager);
        new RedisOpPingParser(redisOpParserManager);
        new RedisOpPublishParser(redisOpParserManager);
        new RedisOpMultiParser(redisOpParserManager);

        server = startFakeXsyncServer(randomPort(), null);
        xsync = new DefaultXsync(getXpipeNettyClientKeyedObjectPool().getKeyPool(new DefaultEndPoint("127.0.0.1", server.getPort())),
                gtidSet, null, scheduled);
        redisOps = new ArrayList<>();
        xsync.addXsyncObserver(this);

        client = AsyncRedisClientFactory.DEFAULT.getOrCreateClient("ApplierTest");
        sequenceController = new DefaultSequenceController();
        sequenceController.initialize();
    }

    @After
    public void tearDown() throws Exception {
        sequenceController.dispose();
    }

    private boolean inTransaction = false;

    @Override
    public void onCommand(Object[] rawCmdArgs) {
        RedisOp redisOp = parser.parse(Stream.of(rawCmdArgs).map(Object::toString).collect(Collectors.toList()));
        if (redisOp instanceof RedisOpPing || redisOp instanceof RedisOpSelect) {
            return;
        }
        ApplierRedisOpCommand<Boolean> command = new DefaultApplierCommand(client, redisOp);
        switch (command.type()) {
            case MULTI:
                inTransaction = true;
            case EXEC:
                inTransaction = false;
            default:
                command.sharding().forEach(sequenceController::submit);
        }
    }
}

@@ -57,45 +57,8 @@
 l;%0A%0A
-import com.ctrip.xpipe.AbstractTest;%0A
 impo
@@ -2611,24 +2611,130 @@
 ver(this);%0A%0A
+        //USE CREDIS%0A        //client = new CRedisAsyncClientFactory().getOrCreateClient(%22ApplierTest%22);%0A%0A
         clie

add comment: how to use credis factory