python-mysql-replication-kr/python-mysql-replication

TestCTLConnectionSettings Logic bug

Closed this issue · 2 comments

Reproduce

TestCTLConnectionSettings Test

Current Behavior

제대로 동작하는것 처럼 보이지만, 실제로는 의도한 바와 다르게 동작중임

def setUp(self):
    super(TestCTLConnectionSettings, self).setUp()
    self.stream.close()
    ctl_db = copy.copy(self.database)
    ctl_db["db"] = None
    ctl_db["port"] = 3307
    self.ctl_conn_control = pymysql.connect(**ctl_db)
    self.ctl_conn_control.cursor().execute("DROP DATABASE IF EXISTS pymysqlreplication_test")
    self.ctl_conn_control.cursor().execute("CREATE DATABASE pymysqlreplication_test")
    self.ctl_conn_control.close()
    ctl_db["db"] = "pymysqlreplication_test"
    self.ctl_conn_control = pymysql.connect(**ctl_db)
    # PRINT ADDED FOR DEBUGGING
    print(ctl_db)
    print(self.database)
    # PRINT ADDED FOR DEBUGGING
    self.stream = BinLogStreamReader(
        self.database,
        ctl_connection_settings=ctl_db,
        server_id=1024,
        only_events=(WriteRowsEvent,),
        fail_on_table_metadata_unavailable=True
    )

ctl_dbself.database를 출력해주는 print를 추가후 실행

> {'host': 'localhost', 'user': 'root', 'passwd': '', 'port': 3307, 'use_unicode': True, 'charset': 'utf8', 'db': 'pymysqlreplication_test'}
> {'host': 'localhost', 'user': 'root', 'passwd': '', 'port': 3306, 'use_unicode': True, 'charset': 'utf8', 'db': 'pymysqlreplication_test'}

원래 테스트의 목적은 self.database라는 dict를 copy.copy를 통해 shallow copy하고 포트를 변경해 3307로 접속하려는 것으로 추정
하지만 BinLogStreamReader에 들어가는 self.database는 부모 클래스의 기본값인 3306으로 접속을 시도

Expected Behavior

3307 포트로 BinLogStreamReader가 접속하도록 변경

Comments

#11 테스트 작성중 발견

Debugging

# base.py
def execute(self, query):
    c = self.conn_control.cursor()
    print(c.connection.port)
    c.execute(query)
    return c

cursor를 이용해 connection.port를 확인

3306
3306

우리가 원하는 3307 포트가 아닌 3306 포트로 실행 되는것을 확인할수 있다

copy.copy를 통한 ctl_db의 생성은 의도된 행동 같다
ctl_connection_settings는 controller db에 연결하는 다른 개념의 Connection이다..
그래서 self.database가 바뀔 필요가 없던것.

대신 Base class의 생성자에 포트를 인자로 받도록 처리하여 다른 포트가 들어올때도 실행 가능하도록 flexible하게 구조를 변경하였다