Skip to content

Instantly share code, notes, and snippets.

View adililhan's full-sized avatar

adililhan

View GitHub Profile
@adililhan
adililhan / non-reentrant-sigaction-local-variable.c
Created October 17, 2022 20:15
non-reentrant function with local variable
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
int sum;
int calculate(int first, int second) {
sum = first + second;
sleep(3); // represent I/O intensive function call
@adililhan
adililhan / non-reentrant.c
Created October 17, 2022 19:37
non-reentrant function
#include <stdio.h>
#include <unistd.h>
int sum;
int calculate(int first, int second) {
sum = first + second;
sleep(3); // represent I/O intensive function call
return sum;
}
@adililhan
adililhan / reentrant-function-sigaction.rs
Created October 15, 2022 20:11
reentrant function in Rust
use nix::sys::signal::*;
use nix::sys::signal::SigAction;
use std::time::Duration;
use std::thread;
fn calculate(first: i32, second: i32) -> i32 {
let sum = first + second;
thread::sleep(Duration::from_secs(3));
sum
}
@adililhan
adililhan / non-reentrant-function.rs
Created October 15, 2022 19:58
non-reentrant function in Rust
use std::thread;
use std::time::Duration;
static mut SUM: i32 = 0;
fn calculate(first: i32, second: i32) -> i32 {
unsafe {
SUM = first + second;
}
thread::sleep(Duration::from_secs(3));
@adililhan
adililhan / tcp_self_connect.c
Created July 17, 2022 01:16
TCP Self-Connect Problem Example
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char const *argv[])
@adililhan
adililhan / unittest.py
Created December 18, 2021 19:19
Correct unit test
@patch.object(
UserController, "__init__", Mock(return_value=None)
)
def test_get_second_picture(self):
mock_app = MagicMock()
mock_app.get_DB.return_value.all.return_value = ["Test 1", "Test 2"]
user = UserController()
user.picture = mock_app
@adililhan
adililhan / unittest.py
Created December 18, 2021 19:17
Wrong unit test
@patch.object(
UserController, "__init__", Mock(return_value=None)
)
def test_get_second_picture(self):
mock_app = MagicMock()
mock_app.get_DB.all.return_value = ["Test 1", "Test 2"]
user = UserController()
user.picture = mock_app
@adililhan
adililhan / user_controller.py
Created December 18, 2021 19:12
MagickMock problem reproduced
class FakeDBAL:
def all(self):
print("all() method called")
return ["Pic1", "Pic2", "Pic3"]
class Picture:
def __init__(self):
print("__init__ method in the Picture class called")
self.dbal = FakeDBAL()
@adililhan
adililhan / unittest.py
Last active December 18, 2021 19:05
Correct unit test
@patch.object(
UserController, "__init__", Mock(return_value=None)
)
def test_get_first_picture(self):
mock_app = MagicMock()
mock_app.dbal.all.return_value = ["Test 1", "Test 2"]
user = UserController()
user.picture = mock_app
@adililhan
adililhan / unittest.py
Last active December 18, 2021 19:04
Wrong unit test
@patch.object(
UserController, "__init__", Mock(return_value=None)
)
def test_get_first_picture(self):
user = UserController()
user.picture = MagicMock()
self.assertEqual("Test 1", user.get_first_picture())