I have the following example of an integral:
$$ I = \frac{1}{4\pi^2}\int_0^{2\pi}\int_0^{2\pi}\frac{\cos^3 x}{\cos x - \cos y} \ dy \ dx $$
I can determine the exact value of this integral by leveraging the symmetry properties of integrals:
$$ I = \frac{1}{2}\left(\frac{1}{4\pi^2}\int_0^{2\pi}\int_0^{2\pi}\frac{\cos^3 x}{\cos x - \cos y} dy dx + \frac{1}{4\pi^2}\int_0^{2\pi}\int_0^{2\pi}\frac{\cos^3 y}{\cos y - \cos x} dx dy\right) $$
$$ = \frac{1}{2}\left(\frac{1}{4\pi^2}\int_0^{2\pi}\int_0^{2\pi}\frac{\cos^3 x - \cos^3 y}{\cos x - \cos y} dy dx\right) $$
$$ = \frac{1}{2}\left(\frac{1}{4\pi^2}\int_0^{2\pi}\int_0^{2\pi}(\cos^2 x + \cos^2 y + \cos x\cos y) dy dx\right) = \frac{1}{2} $$
In my case, I need to evaluate the first integral with respect to $y$:
$$ I(x) = \frac{1}{2\pi}\int_0^{2\pi}\frac{1}{\cos x - \cos y} dy $$
and then integrate over $x$:
$$ I = \frac{1}{2\pi}\int_0^{2\pi}I(x)\cos^3 x dx $$ However, the results are not valid. What mistake did I make?
Too long for a comment
The answer is that in the case of your integral it is not allowed to change the order of integration. $$I_1=\frac{1}{4\pi^2}\int_0^{2\pi}\int_0^{2\pi}\frac{\cos^3 x}{\cos x - \cos y} \ dy \ dx= \frac{1}{4\pi^2}\int_0^{2\pi}\left(\int_0^{2\pi}\frac{dy}{\cos x - \cos y} \right) \cos^3x\ dx$$ $$I_2=\frac{1}{4\pi^2}\int_0^{2\pi}\int_0^{2\pi}\frac{\cos^3 x}{\cos x - \cos y} \ dx \ dy= \frac{1}{4\pi^2}\int_0^{2\pi}\left(\int_0^{2\pi}\frac{\cos^3x}{\cos x - \cos y} \ dx\right)\ dy$$ Renaming in the second integral $x$ and $y$ $$I_2=\frac{1}{4\pi^2}\int_0^{2\pi}\left(\int_0^{2\pi}\frac{\cos^3y}{\cos y - \cos x} \ dy\right)\ dx$$ It is straightforward to see that $I_1\neq I_2$
Supposing that $\,\displaystyle \int_0^{2\pi}\frac{\cos^3y}{\cos y - \cos x} \ dy\,$ exists in the principal value sense (in fact it does) $$\int_0^{2\pi}\frac{\cos^3y}{\cos y - \cos x} \ dy=\int_0^{2\pi}\frac{\cos^2y\,(\cos y-\cos x+\cos x)}{\cos y - \cos x} \ dy$$ $$=\int_0^{2\pi}\cos^2y\ dy+\cos x\int_0^{2\pi}\frac{\cos y\,(\cos y-\cos x+\cos x)}{\cos y - \cos x} \ dy$$ $$=\int_0^{2\pi}\cos^2y\ dy+\cos x\int_0^{2\pi}\cos y\ dy+\cos^2 x\int_0^{2\pi}\ dy+\cos^3 x\int_0^{2\pi}\frac{dy}{\cos y - \cos x}$$ $$=\pi+2\pi \cos^2x+\cos^3 x\int_0^{2\pi}\frac{dy}{\cos y - \cos x}$$ Therefore, $$I_2=\frac1{4\pi^2}\int_0^{2\pi}\left(\pi+2\pi \cos^2x+\cos^3 x\int_0^{2\pi}\frac{dy}{\cos y - \cos x}\right)dx$$ $$=1+\frac1{4\pi^2}\int_0^{2\pi}\left(\int_0^{2\pi}\frac{dy}{\cos y - \cos x}\right)\cos^3x \ dx=1-I_1$$ $$\quad\Rightarrow\quad \frac12\Big(I_1+I_2\Big)=\frac12$$ Also, in the principal value sense $$P.V.\int_0^{2\pi}\frac{dy}{\cos x - \cos y} =2i\,P.V.\oint_{|z|=1}\frac{dz}{z^2-2\cos x\,z+1}$$ $$=2i\,P.V.\oint_{|z|=1}\frac{dz}{(z-e^{ix})(z-e^{-ix})}$$ where we integrate in the positive (counter-clockwise) direction.
Adding to the contour two small half-circles (clockwise) around $z=e^{\pm ix}$ (closing the contour and leaving the poles outside the contour), and evaluating the integrals along these small arches (evaluating half-residues at $z=e^{\pm ix}$), we get zero.
It follows that $I_1=0$