Files
2nd/10_Wiki/Topics/Architecture/Preprocessing Seam (전처리 접점).md
T
2026-05-10 22:08:15 +09:00

144 lines
4.2 KiB
Markdown

---
id: wiki-2026-0508-preprocessing-seam-전처리-접점
title: Preprocessing Seam (전처리 접점)
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Preprocessing Seam, 전처리 접점, Feathers Seam, link seam]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [legacy-code, testing, refactoring, seam]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: c-cpp-python
framework: legacy-refactoring
---
# Preprocessing Seam (전처리 접점)
## 매 한 줄
> **"매 컴파일 전에 매 동작을 바꾸는 매 접점"**. Preprocessing seam은 Michael Feathers "Working Effectively with Legacy Code" (2004) 의 매 3대 seam (preprocessing / link / object) 중 매 첫 번째 — 매 source 수정 없이 매 매크로/include 치환으로 매 test isolation 가능 지점. C/C++ 같은 매 preprocessor-heavy language의 매 testability 회복 도구.
## 매 핵심
### 매 Seam 정의
- **매 Seam**: 매 source 수정 없이 매 동작 변경 가능한 매 지점.
- **매 Enabling point**: 매 seam을 매 활용하는 매 mechanism (here: preprocessor).
- **매 3종류**: preprocessing / link / object.
### 매 Preprocessing seam 작동
- `#include "db.h"` → 매 test 시 매 fake `db.h` include path 우선.
- `#define malloc test_malloc` → 매 allocator hook.
- `#ifdef TESTING` 매 conditional compile.
### 매 응용
1. Legacy C codebase 매 unit test 추가.
2. Embedded firmware의 매 hardware register hook.
3. Time/random 매 deterministic replacement.
## 💻 패턴
### Header substitution seam
```c
// production: src/db.h
int db_query(const char* sql);
// test/fakes/db.h — 매 same signature, 매 fake impl
int db_query(const char* sql) { return 42; }
// CMake test target
target_include_directories(my_test PRIVATE test/fakes src) // 매 fakes 우선
```
### Macro override
```c
// production: alloc.h
#ifndef TESTING
# define MY_MALLOC malloc
# define MY_FREE free
#else
# define MY_MALLOC test_malloc
# define MY_FREE test_free
#endif
void* p = MY_MALLOC(64);
```
### Time seam (deterministic test)
```c
// time_seam.h
#ifdef TESTING
extern uint64_t mock_now;
# define NOW_MS() (mock_now)
#else
# define NOW_MS() ((uint64_t)(clock_gettime_ms()))
#endif
// test
uint64_t mock_now = 1000;
mock_now += 500;
ASSERT(elapsed() == 500);
```
### Linker seam (sibling)
```c
// production link: real_sensor.o
int read_sensor(void);
// test link: fake_sensor.o 매 same symbol, 매 different impl
int read_sensor(void) { return injected_value; }
// gcc test.c fake_sensor.o -o test // 매 real_sensor.o 미포함
```
### Python equivalent — sys.modules patch
```python
# 매 import-time seam
import sys, types
fake = types.ModuleType("db")
fake.query = lambda sql: 42
sys.modules["db"] = fake
import service # 매 service.py 의 `import db` → fake
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| C/C++ legacy, 매 source 매 못 건드림 | Preprocessing seam |
| Static lib, 매 link-time control | Link seam |
| Java/.NET runtime | Object seam (subclass / DI) |
| Python | sys.modules / monkeypatch |
**기본값**: Object seam 우선, 매 안 되면 link → preprocessing 순.
## 🔗 Graph
- 부모: [[Legacy-Code]] · [[Seam]]
- 변형: [[Link-Seam]] · [[Object-Seam]]
- 응용: [[Unit-Testing]] · [[Embedded-Testing]] · [[Mock-Object]]
- Adjacent: [[Feathers-WELC]] · [[Dependency-Injection]] · [[Test-Double]]
## 🤖 LLM 활용
**언제**: legacy C/C++ 매 testability 회복, 매 hardware seam 설계.
**언제 X**: 매 greenfield project (DI 우선).
## ❌ 안티패턴
- **매 #ifdef TESTING 폭주**: 매 production code 매 #ifdef 매 가독성 파괴.
- **매 Header path race**: 매 fake include order 매 fragile.
- **매 Macro hell**: 매 함수 매크로 매 debug 불가.
- **매 매 seam 영구화**: 매 test seam 을 매 production refactor 후에도 매 유지.
## 🧪 검증 / 중복
- Verified (Feathers "Working Effectively with Legacy Code" 2004 Ch.4).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — Feathers 3-seam taxonomy + working C/Python examples |