After completing the debugging of the Socket protocol interface, the next step is to continue the business test of the Socket interface. Because the current demand received is a function for teacher and student to chat with each other, it must be tested through the long connection between two users. After some attempts and modifications, the basic script has been completed and shared as follows for reference only.

Previous Socket articles:

  • Socket interface development and testing
  • Client encapsulation based on WebSocket
  • Client encapsulation based on socket. IO

Train of thought

The current service logic sequence is as follows:

  • HTTPThe login
  • throughtokenanduidEstablishing a long connection
  • registerLong connection user
  • joinThe room
  • Send a message
  • Shut downSocket

The original plan was to use two threads to complete the test, but later found that this is different from WebSocket, and can complete the test without multithreading. In the last step, the sleep(second) and closeAll() methods work together.

structure

Divided into three categories:

  • The configuration class
  • Basic function class
  • The script

Configuration class mainly stores configuration information, for multi-environment configuration, there is nothing to talk about. Basic function class, mainly to complete the various types of send object encapsulation, I used Groovy to write, because it is too cool, you can see the code. Script class is to write various test scripts, provide testing and data creation functions.

Since I am not familiar with the Socket of the modified project and have not optimized the project, the constant parameters in the script will be cancelled in the later encapsulation, which is more convenient for testing.

code

Configuration classes are omitted because there is really nothing to talk about.

Basic function class

At present, only a part of the work has been done, and a large number of functions need to be followed up.

package com.okayqa.socket.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fun.frame.SourceCode;

/** * Socket base class */
class SocketBase extends SourceCode {

    /** * register ** @param uid * @param token * @param isS * @return */
    public static JSONObject getRegister(long uid, String token, boolean isS) {
        JSON.parseObject("{\"cmd\": \"register\", \"userId\": ${uid}, \"role\": \"${isS ? "S":"T"} \ "and \" deviceVersion \ ": \" 1.0 \ ", \ "s_sid \" : 123, \ "token \" : \ "${token} \"}");
    }

    /** * join room ** @param roomId * @return */
    public static JSONObject getJoinRoom(int roomId) {
        JSON.parseObject("{\"cmd\": \"joinRoom\", \"roomId\": ${roomId}}")}/** * @param activity_id * @param from * @param to * @param kid * @param ktype * @param MSG * @param S2T * @return */
    public static JSONObject getChat(int activity_id, long from, long to, int kid, int ktype, String msg, boolean S2T) {
        JSON.parseObject("{\"cmd\":\"chat\",\"message\":{\"sender\":${from},\"receiver\":${to},\"body\":\"${msg}\",\"msgtype\":\"chat\",\"mimetyp e\":\"text\",\"kid\":${kid},\"ktype\":${ktype},\"activity_id\":${activity_id},\"sender_name\":\"SENDER_NAME\",\"sender_r ole\":\"${S2T ? "student":"teacher"}\",\"receiver_name\":\"RECEIVER_NAME\",\"receiver_role\":\"${S2T ? "student":"teacher"} \ "}}")}/** * @param actitiviId * @param status The status is 0. There is one screen holding and two screen locking * @return */
    public static JSONObject getScreenStatus(int actitiviId, int status) {
        JSON.parseObject("{\"cmd\":\"screenStatus\", \"activityId\":${actitiviId}, \"minicourseId\":int,\"status\":${status}")}}Copy the code

The script class

Only wrote a teacher and students chat logic test script, to provide your reference.

package com.okayqa.socket.test


import com.fun.frame.socket.ScoketIOFunClient
import com.fun.utils.Time
import com.okayqa.socket.base.SocketBase
import com.okayqa.socket.profile.OkayScoketConstant
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/** * wiki:http://wiki.okjiaoyu.cn/display/RJBK/ailearn-instruction-svr */
class ST extends SocketBase {

    private static Logger logger = LoggerFactory.getLogger(ST.class)

    static int roomId = 42422;

    static int activity_id = roomId;

    public static void main(String[] args) {

        def tbase = com.okayqa.teacherpad.base.OkayBase.getBase()
        def sbase = com.okayqa.studentpad.base.OkayBase.getBase()

        ScoketIOFunClient teacher = ScoketIOFunClient.getInstance(OkayScoketConstant.HOST + "/? systemId=${tbase.getUid()}&loginType=3&token=${tbase.getToken()}&userType=1"."The teacher ${tbase. GetUid ()}");
        teacher.connect();
        teacher.addEventListener(OkayScoketConstant.RESPONSE, { objects ->
            String s = ScoketIOFunClient.initMsg(objects);
            logger.info("{} Received :{}", teacher.getCname(), s);
        });

        ScoketIOFunClient student = ScoketIOFunClient.getInstance(OkayScoketConstant.HOST + "/? systemId=${sbase.getUname()}&loginType=3&token=${sbase.getToken()}&userType=2"."Students ${sbase. GetUname ()}");
        student.connect();
        student.addEventListener(OkayScoketConstant.RESPONSE, { objects ->
            String s = ScoketIOFunClient.initMsg(objects);
            logger.info("{} Received :{}", student.getCname(), s);
        });

        teacher.send(OkayScoketConstant.EVENT, getRegister(tbase.getUid(), tbase.getToken(), false));
        teacher.send(OkayScoketConstant.EVENT, getJoinRoom(roomId));
        student.send(OkayScoketConstant.EVENT, getRegister(sbase.getUname() as long, sbase.getToken(), true));
        student.send(OkayScoketConstant.EVENT, getJoinRoom(roomId));



        teacher.send(OkayScoketConstant.EVENT, getChat(activity_id, tbase.getUid(), sbase.getUname() as long.82.0."Guess who I am." + Time.getDate(), false));
        student.send(OkayScoketConstant.EVENT, getChat(activity_id, sbase.getUname() as long, tbase.getUid(), 82.0."Guess who I am." + Time.getDate(), true));



        sleep(10_000);

        logger.info("Script complete!")
        ScoketIOFunClient.closeAll()
    }


}

Copy the code

The console

INFO->The current user: fv, IP: 10.60.192.21, working directory: / Users/fv/Documents/workspace/okay_test, system coding format: utf-8, Mac OS X system version: 10.15.7
INFO-> requestid: Fdev1606988150561
INFO->The request URI is https://teacherpad-stress.xk12.cn/api/t_pad/user/login, and the time is 910 ms
INFO->Teacher: 61951375269, subject: NULL, Name: Teacher Fan zero zero, login successful!
INFO-> requestid: Fdev1606988151672
INFO->Request uri: https://stupad-stress.xk12.cn/api/pad/user/login, time-consuming: 387 ms
INFO->User: 81951375949, login successful!
INFO->Teacher 61951375269 Start connecting...
INFO->Teacher 61951375269 Connection successful!
INFO->Student 81951375949 Start connecting...
INFO->Student 81951375949 connection successful!... leave out some informationINFO->Script complete!
INFO->Teacher 61951375269 Socket link down!
INFO->Student 81951375949 Socket link down!
INFO->Close all Socket clients!

Process finished with exit code 0

Copy the code

Since there are so many messages returned, just take a screenshot and look at it.


The public,FunTester, non-famous test development, the article records learning and comprehension, welcome to pay attention to, exchange growth.

FunTester hot text selection

  • Selenium4 IDE, it’s finally here
  • Dom-based XML file parsing class
  • How to become a Full stack automation engineer
  • Bind mobile phone number performance test
  • Exploration of fixed QPS pressure measurement mode
  • Why do tests miss bugs
  • Automated test lifecycle
  • Fixed QPS pressure test
  • Gradle + Groovy basis
  • Gradle plus Groovy increases